Class ListSearchable

java.lang.Object
com.jidesoft.swing.Searchable
com.jidesoft.swing.ListSearchable
All Implemented Interfaces:
PropertyChangeListener, EventListener, ListDataListener

public class ListSearchable extends Searchable implements ListDataListener, PropertyChangeListener
ListSearchable is an concrete implementation of Searchable that enables the search function in JList.

It's very simple to use it. Assuming you have a JList, all you need to do is to call

 JList list = ....;
 ListSearchable searchable = new ListSearchable(list);
 
Now the JList will have the search function.

There is very little customization you need to do to ListSearchable. The only thing you might need is when the element in the JList needs a special conversion to convert to string. If so, you can overide convertElementToString() to provide you own algorithm to do the conversion.

 JList list = ....;
 ListSearchable searchable = new ListSearchable(list) {
      protected String convertElementToString(Object object) {
          ...
      }
 };
 

Additional customization can be done on the base Searchable class such as background and foreground color, keystrokes, case sensitivity.

JList actually has a simple searchable feature but has flaws. It will affect our searchable feature. To workaround it, you can override getNextMatch method and always return -1 when you create your JList.

 JList list = new JList(...) {
     public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
         return -1;
     }
 };
 
  • Constructor Details

    • ListSearchable

      public ListSearchable(JList list)
  • Method Details

    • uninstallListeners

      public void uninstallListeners()
      Description copied from class: Searchable
      Uninstall the listeners that installed before. This method is never called because we don't have the control of the life cycle of the component. However you can call this method if you don't want the component to be searchable any more.
      Overrides:
      uninstallListeners in class Searchable
    • setSelectedIndex

      public void setSelectedIndex(int index, boolean incremental)
      Description copied from class: Searchable
      Sets the selected index. The concrete implementation should call methods on the component to select the element at the specified index. The incremental flag is used to do multiple select. If the flag is true, the element at the index should be added to current selection. If false, you should clear previous selection and then select the element.
      Specified by:
      setSelectedIndex in class Searchable
      Parameters:
      index - the index to be selected
      incremental - a flag to enable multiple selection. If the flag is true, the element at the index should be added to current selection. If false, you should clear previous selection and then select the element.
    • getSelectedIndex

      protected int getSelectedIndex()
      Description copied from class: Searchable
      Gets the selected index in the component. The concrete implementation should call methods on the component to retrieve the current selected index. If the component supports multiple selection, it's OK just return the index of the first selection.

      Here are some examples. In the case of JList, the index is the row index. In the case of JTree, the index is the row index too. In the case of JTable, depending on the selection mode, the index could be row index (in row selection mode), could be column index (in column selection mode) or could the cell index (in cell selection mode).

      Specified by:
      getSelectedIndex in class Searchable
      Returns:
      the selected index.
    • getElementAt

      protected Object getElementAt(int index)
      Description copied from class: Searchable
      Gets the element at the specified index. The element could be any data structure that internally used in the component. The convertElementToString method will give you a chance to convert the element to string which is used to compare with the string that user types in.
      Specified by:
      getElementAt in class Searchable
      Parameters:
      index - the index
      Returns:
      the element at the specified index.
    • getElementCount

      protected int getElementCount()
      Description copied from class: Searchable
      Gets the total element count in the component. Different concrete implementation could have different interpretation of the count. This is totally OK as long as it's consistent in all the methods. For example, the index parameter in other methods should be always a valid value within the total count.
      Specified by:
      getElementCount in class Searchable
      Returns:
      the total element count.
    • convertElementToString

      protected String convertElementToString(Object object)
      Converts the element in Jlist to string. The returned value will be the toString() of whatever element that returned from list.getModel().getElementAt(i).
      Specified by:
      convertElementToString in class Searchable
      Parameters:
      object - the object to be converted to string
      Returns:
      the string representing the element in the JList.
    • contentsChanged

      public void contentsChanged(ListDataEvent e)
      Specified by:
      contentsChanged in interface ListDataListener
    • intervalAdded

      public void intervalAdded(ListDataEvent e)
      Specified by:
      intervalAdded in interface ListDataListener
    • intervalRemoved

      public void intervalRemoved(ListDataEvent e)
      Specified by:
      intervalRemoved in interface ListDataListener
    • propertyChange

      public void propertyChange(PropertyChangeEvent evt)
      Specified by:
      propertyChange in interface PropertyChangeListener
    • isUseRendererAsConverter

      public boolean isUseRendererAsConverter()
      Get the flag if the ListSearchable should use the renderer in the list as its converter.

      The default value for this field is false so we can get higher performance. For AutoFilterBox, we will set it to false automatically.

      Returns:
      true if you want to use the renderer as its converter. Otherwise false.
    • setUseRendererAsConverter

      public void setUseRendererAsConverter(boolean useRendererAsConverter)
      Set the flag if the ListSearchable should use the renderer in the list as its converter.

      Parameters:
      useRendererAsConverter - the flag
      See Also: