How To Add History To A JYSearchField(14:49, 26. Apr. 2011)

As you've maybe noticed we use a customized JYSearchField with a search history in our SyntheticaTunes demo application.

SearchFieldHistory - JYSearchField

To enable a history popup we specify a JYPopupMenu with a maximium of 10 visible elements and set the popup to the search field. When more than 10 elements will be added a scrollbar appears.

JYSearchField searchField = new HistorySearchField(20);
searchField.setSearchPopupButtonEnabled(true);
searchField.setSearchAction(new SearchAction());
JYPopupMenu popup = new JYPopupMenu();
popup.setVisibleElements(10);
searchField.setSearchPopupMenu(popup);    

To manage the history content we create a customized search field which extends JYSearchField. The updateHistory() method adds the current value to the top of the list and removes old entries from the list when the maximum size is exceeded.

private static class HistorySearchField extends JYSearchField
{
  private static int maxItems = 50;

  public HistorySearchField(int columns)
  {
    super(columns);
  }
    
  public void updateHistory()
  {
    String searchText = getText().trim();
    if (searchText.length() == 0)
      return;
      
    //cache items
    JYPopupMenu popup = (JYPopupMenu)getSearchPopupMenu();
    JPanel p = popup.getMenuElementPanel();
    ArrayList<JMenuItem> itemList = new ArrayList<JMenuItem>();
    for (Component c : p.getComponents())
      if (c instanceof JMenuItem)
        itemList.add((JMenuItem)c);
    p.removeAll();
      
    //add new item at top
    JMenuItem newItem = new JMenuItem(new AbstractAction(searchText)
    {      
      public void actionPerformed(ActionEvent evt)
      {
        setText((String)getValue(NAME)); 
        getSearchAction().actionPerformed(new SearchActionEvent(HistorySearchField.this, ActionEvent.ACTION_PERFORMED, "", System.currentTimeMillis(), 0, Initiator.SEARCH_FIELD));
      }
    });      
    popup.add(newItem);
      
    //add old items
    for (JMenuItem mi : itemList)
    {
      if (p.getComponentCount() >= maxItems)
        break;
      if (!searchText.equals(mi.getText()))
        popup.add(mi);
    }
  }     
}

In the last step we have to update the history each time the search action is performed.

private static class SearchAction extends AbstractAction
{
  public void actionPerformed(ActionEvent evt)
  {
    //do not execute on button click
    if (((SearchActionEvent)evt).getInitiator() == Initiator.SEARCH_POPUP_BUTTON)
      return;      
      
    HistorySearchField searchField = (HistorySearchField)evt.getSource();
    searchField.updateHistory();
    String searchText = searchField.getText();
    //...add code to perform search
  }    
}

That's it - below you find a simple application which demonstrates the customized JYSearchField.

WebStart Demo

Download Demo Sourcecode

Note: JYSearchField and JYPopupMenu are part of the SyntheticaAddons package.

Related Posts

How To Create A Flat Button Style
Customization - How to apply a Firefox style to a JTabbedPane