Class Searchable

  • Direct Known Subclasses:
    ComboBoxSearchable, ListSearchable, TableSearchable, TextComponentSearchable, TreeSearchable

    public abstract class Searchable
    extends java.lang.Object
    JList, JTable and JTree are three data-rich components. They can be used to display a huge amount of data so searching function will be very a useful feature in those components. Searchable is such a class that can make JList, JTable and JTree searchable. User can simply type in any string they want to search for and use arrow keys to navigate to next or previous occurrence.

    Searchable is a base abstract class. ListSearchable, TableSearchable and TreeSearchable are implementations to make JList, JTable and JTree searchable respectively. For each implementation, there are five methods need to be implemented.

    • protected abstract int getSelectedIndex()
    • protected abstract void setSelectedIndex(int index, boolean incremental)
    • protected abstract int getElementCount()
    • protected abstract Object getElementAt(int index)
    • protected abstract String convertElementToString(Object element)

    Please look at the javadoc of each method to learn more details.

    The keys used by this class are fully customizable. Subclass can override the methods such as isActivateKey(java.awt.event.KeyEvent), isDeactivateKey(java.awt.event.KeyEvent), isFindFirstKey(java.awt.event.KeyEvent),isFindLastKey(java.awt.event.KeyEvent), isFindNextKey(java.awt.event.KeyEvent), isFindPreviousKey(java.awt.event.KeyEvent) to provide its own set of keys.

    In addition to press up/down arrow to find next occurrence or previous occurrence of particular string, there are several other features that are very handy.

    Multiple selection feature - If you press CTRL key and hold it while pressing up and down arrow, it will find next/previous occurrence while keeping existing selections.
    Select all feature - If you type in a searching text and press CTRL+A, all the occurrences of that searching string will be selected. This is a very handy feature. For example you want to delete all rows in a table whose name column begins with "old". So you can type in "old" and press CTRL+A, now all rows beginning with "old" will be selected. Pressing delete will delete all of them.
    Basic regular expression support - It allows '?' to match any letter or digit, or '*' to match several letters or digits. Even though it's possible to implement full regular expression support, we don't want to do that. The reason is the regular expression is very complex, it's probably not a good idea to let user type in such a complex expression in a small popup window. However if your user is very familiar with regular expression, you can add the feature to Searchable. All you need to do is to override compare(String, String) method and implement by yourself.

    As this is an abstract class, please refer to to javadoc of ListSearchable,TreeSearchable, and TableSearchable to find out how to use it with JList, JTree and JTable respectively.

    This component has a timer. If user types very fast, it will accumulate them together and generate only one searching action. The timer can be controlled by setSearchingDelay(int).

    By default we will use lightweight popup for the sake of performance. But if you use heavyweight component which could obscure the lightweight popup, you can call setHeavyweightComponentEnabled(boolean) to true so that heavyweight popup will be used.

    When a Searchable is installed on a component, component.getClientProperty(Searchable.CLIENT_PROPERTY_SEARCHABLE) will give you the Searchable instance. You can use static method getSearchable(javax.swing.JComponent) to get it too.

    Last but not the least, only one Searchable is allowed on a component. If you install another one, it will remove the first one and then install the new one.

    • Constructor Summary

      Constructors 
      Constructor Description
      Searchable​(javax.swing.JComponent component)
      Creates a Searchable.
      Searchable​(javax.swing.JComponent component, SearchableProvider searchableProvider)
      Creates a Searchable.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addPropertyChangeListener​(java.beans.PropertyChangeListener propertychangelistener)
      Adds the property change listener.
      void addSearchableListener​(SearchableListener l)
      Adds the specified listener to receive searchable events from this searchable.
      void adjustSelectedIndex​(int index, boolean incremental)
      Sets the selected index.
      protected void cancelHighlightAll()
      Cancel highlight all.
      protected boolean compare​(java.lang.Object element, java.lang.String searchingText)
      Checks if the element matches the searching text.
      protected boolean compare​(java.lang.String text, java.lang.String searchingText)
      Checks if the element string matches the searching text.
      protected abstract java.lang.String convertElementToString​(java.lang.Object element)
      Converts the element that returns from getElementAt() to string.
      java.lang.String convertToString​(java.lang.Object element)
      Converts the element to String.
      protected java.awt.event.ComponentListener createComponentListener()
      Creates a component listener that updates the popup when component is hidden, moved or resized.
      protected java.awt.event.FocusListener createFocusListener()
      Creates a FocusListener.
      protected java.awt.event.KeyListener createKeyListener()
      Creates the KeyListener and listen to key typed in the component.
      protected Searchable.SearchPopup createSearchPopup​(java.lang.String searchingText)
      Creates the popup to hold the searching text.
      java.util.List<java.lang.Integer> findAll​(java.lang.String s)
      findAll uses the Searchable to find all the element indices that match the searching string.
      int findFirst​(java.lang.String s)
      Finds the first element that matches the searching text.
      int findFirstExactly​(java.lang.String s)
      Finds the first element that matches the searching text exactly.
      int findFromCursor​(java.lang.String s)
      Finds the next matching index from the cursor.
      int findLast​(java.lang.String s)
      Finds the last element that matches the searching text.
      int findNext​(java.lang.String s)
      Finds the next matching index from the cursor.
      int findPrevious​(java.lang.String s)
      Finds the previous matching index from the cursor.
      void firePropertyChangeEvent​(java.lang.String searchingText)  
      protected void fireSearchableEvent​(SearchableEvent e)
      Fires a searchable event.
      java.awt.Color getBackground()
      Gets the background color used inn the search popup.
      java.awt.Component getComponent()
      Gets the actual component which installed this Searchable.
      protected int getCurrentIndex()  
      int getCursor()
      Gets the cursor which is the index of current location when searching.
      protected abstract java.lang.Object getElementAt​(int index)
      Gets the element at the specified index.
      java.lang.String getElementAtAsString​(int index)
      Gets the element at the specified index as string using convertElementToString(Object) method.
      protected abstract int getElementCount()
      Gets the total element count in the component.
      java.awt.Color getForeground()
      Gets the foreground color used inn the search popup.
      java.awt.Color getMismatchForeground()
      Gets the foreground color when the searching text doesn't match with any of the elements in the component.
      int getPopupLocation()
      Gets the popup location.
      java.awt.Component getPopupLocationRelativeTo()
      Gets the component that the location of the popup relative to.
      int getPopupTimeout()
      Gets the timeout for showing the popup.
      protected java.lang.String getResourceString​(java.lang.String key)
      Gets the localized string from resource bundle.
      static Searchable getSearchable​(javax.swing.JComponent component)
      Gets the Searchable installed on the component.
      SearchableListener[] getSearchableListeners()
      Returns an array of all the SearchableListeners added to this SearchableGroup with addSearchableListener.
      SearchableProvider getSearchableProvider()  
      int getSearchingDelay()
      If it returns a positive number, it will wait for that many ms before doing the search.
      java.lang.String getSearchingText()
      Gets the searching text.
      java.lang.String getSearchLabel()
      Gets the current text that appears in the search popup.
      protected abstract int getSelectedIndex()
      Gets the selected index in the component.
      WildcardSupport getWildcardSupport()
      Gets the WildcardSupport.
      void hidePopup()
      Hides the popup.
      protected void highlightAll()
      Highlight all matching cases in the target.
      void installListeners()
      Installs necessary listeners to the component.
      protected boolean isActivateKey​(java.awt.event.KeyEvent e)
      Checks if the key in KeyEvent should activate the search popup.
      boolean isCaseSensitive()
      Checks if the case is sensitive during searching.
      boolean isCountMatch()
      Gets the flag indicating if the Searchable should count all matches for every search.
      protected boolean isDeactivateKey​(java.awt.event.KeyEvent e)
      Checks if the key in KeyEvent should hide the search popup.
      protected boolean isFindFirstKey​(java.awt.event.KeyEvent e)
      Checks if the key is used as a key to find the first occurrence.
      protected boolean isFindLastKey​(java.awt.event.KeyEvent e)
      Checks if the key is used as a key to find the last occurrence.
      protected boolean isFindNextKey​(java.awt.event.KeyEvent e)
      Checks if the key is used as a key to find the next occurrence.
      protected boolean isFindPreviousKey​(java.awt.event.KeyEvent e)
      Checks if the key is used as a key to find the previous occurrence.
      boolean isFromStart()
      This is a property of how to compare searching text with the data.
      boolean isHeavyweightComponentEnabled()  
      boolean isHideSearchPopupOnEvent()
      Get the flag indicating if the search popup should be hidden on the component's event.
      protected boolean isIncrementalSelectKey​(java.awt.event.KeyEvent e)
      Checks if the key will trigger incremental selection.
      protected boolean isNavigationKey​(java.awt.event.KeyEvent e)
      Checks if the key is used as a navigation key.
      boolean isPopupVisible()
      Check if the searchable popup is visible.
      boolean isProcessModelChangeEvent()
      Get the flag if we should process model change event.
      boolean isRepeats()
      Checks if restart from the beginning when searching reaches the end or restart from the end when reaches beginning.
      boolean isReverseOrder()
      Checks the searching order.
      boolean isSearchableListenerInstalled​(SearchableListener l)
      Returns if a given listener is already installed.
      protected boolean isSelectAllKey​(java.awt.event.KeyEvent e)
      Checks if the key will trigger selecting all.
      boolean isWildcardEnabled()
      Checks if it supports wildcard in searching text.
      protected void keyTypedOrPressed​(java.awt.event.KeyEvent e)
      This method is called when a key is typed or pressed.
      void removePropertyChangeListener​(java.beans.PropertyChangeListener propertychangelistener)
      Removes the property change listener.
      void removeSearchableListener​(SearchableListener l)
      Removes the specified searchable listener so that it no longer receives searchable events.
      int reverseFindFromCursor​(java.lang.String s)
      Finds the previous matching index from the cursor.
      protected void searchingTextEmpty()
      Actions to take on searching text empty scenario
      protected void select​(int index, java.lang.String searchingText)
      Select the index for the searching text.
      void setBackground​(java.awt.Color background)
      Sets the background color used by popup.
      void setCaseSensitive​(boolean caseSensitive)
      Sets the case sensitive flag.
      void setCountMatch​(boolean countMatch)
      Sets the flag indicating if the Searchable should count all matches for every search.
      void setCursor​(int cursor)
      Sets the cursor which is the index of current location when searching.
      void setCursor​(int cursor, boolean incremental)
      Sets the cursor which is the index of current location when searching.
      void setForeground​(java.awt.Color foreground)
      Sets the foreground color used by popup.
      void setFromStart​(boolean fromStart)
      Sets the fromStart property.
      void setHeavyweightComponentEnabled​(boolean heavyweightComponentEnabled)  
      void setHideSearchPopupOnEvent​(boolean hideSearchPopupOnEvent)
      Set the flag indicating if the search popup should be hidden on the component's event.
      void setMismatchForeground​(java.awt.Color mismatchForeground)
      Sets the foreground for mismatch.
      void setPopupLocation​(int popupLocation)
      Sets the popup location.
      void setPopupLocationRelativeTo​(java.awt.Component popupLocationRelativeTo)
      Sets the location of the popup relative to the specified component.
      void setPopupTimeout​(int popupTimeout)
      Sets the timeout for showing the popup.
      void setProcessModelChangeEvent​(boolean processModelChangeEvent)
      Set the flag if we should process model change event.
      void setRepeats​(boolean repeats)
      Sets the repeat flag.
      void setReverseOrder​(boolean reverseOrder)
      Sets the searching order.
      void setSearchableProvider​(SearchableProvider searchableProvider)  
      void setSearchingDelay​(int searchingDelay)
      If this flag is set to a positive number, it will wait for that many ms before doing the search.
      void setSearchLabel​(java.lang.String searchLabel)
      Sets the text that appears in the search popup.
      protected abstract void setSelectedIndex​(int index, boolean incremental)
      Sets the selected index.
      void setWildcardEnabled​(boolean wildcardEnabled)
      Enable or disable the usage of wildcard.
      void setWildcardSupport​(WildcardSupport wildcardSupport)
      Sets the WildcardSupport.
      void showPopup​(java.lang.String searchingText)
      Shows the search popup.
      protected void textChanged​(java.lang.String text)  
      void uninstallListeners()
      Uninstall the listeners that installed before.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _component

        protected final javax.swing.JComponent _component
      • _matchCount

        protected int _matchCount
      • _componentListener

        protected java.awt.event.ComponentListener _componentListener
      • _keyListener

        protected java.awt.event.KeyListener _keyListener
      • _focusListener

        protected java.awt.event.FocusListener _focusListener
      • PROPERTY_SEARCH_TEXT

        public static final java.lang.String PROPERTY_SEARCH_TEXT
        See Also:
        Constant Field Values
      • listenerList

        protected javax.swing.event.EventListenerList listenerList
        A list of event listeners for this component.
      • CLIENT_PROPERTY_SEARCHABLE

        public static final java.lang.String CLIENT_PROPERTY_SEARCHABLE
        The client property for Searchable instance. When Searchable is installed on a component, this client property has the Searchable.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Searchable

        public Searchable​(javax.swing.JComponent component)
        Creates a Searchable.
        Parameters:
        component - component where the Searchable will be installed.
      • Searchable

        public Searchable​(javax.swing.JComponent component,
                          SearchableProvider searchableProvider)
        Creates a Searchable.
        Parameters:
        component - component where the Searchable will be installed.
        searchableProvider - the Searchable Provider.
    • Method Detail

      • getSelectedIndex

        protected abstract int getSelectedIndex()
        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).

        Returns:
        the selected index.
      • setSelectedIndex

        protected abstract void setSelectedIndex​(int index,
                                                 boolean incremental)
        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.
        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.
      • adjustSelectedIndex

        public void adjustSelectedIndex​(int index,
                                        boolean incremental)
        Sets the selected index. The reason we have this method is just for back compatibility. All the method do is just to invoke setSelectedIndex(int, boolean).

        Please do NOT try to override this method. Always override setSelectedIndex(int, boolean) instead.

        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.
      • getElementCount

        protected abstract int getElementCount()
        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.
        Returns:
        the total element count.
      • getElementAt

        protected abstract java.lang.Object getElementAt​(int index)
        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.
        Parameters:
        index - the index
        Returns:
        the element at the specified index.
      • convertElementToString

        protected abstract java.lang.String convertElementToString​(java.lang.Object element)
        Converts the element that returns from getElementAt() to string.
        Parameters:
        element - the element to be converted
        Returns:
        the string representing the element in the component.
      • convertToString

        public java.lang.String convertToString​(java.lang.Object element)
        Converts the element to String.

        This method will invoke convertElementToString(Object) only. This method is added to provide a public method for ShrinkSearchSupport without breaking the existing code of the customers.

        Parameters:
        element - the element to be converted
        Returns:
        the string representing the element in the component.
        Since:
        3.4.5
      • isHideSearchPopupOnEvent

        public boolean isHideSearchPopupOnEvent()
        Get the flag indicating if the search popup should be hidden on the component's event.

        By default, the value is true so that the search popup will be hidden anyway when the component get related events. However, you could set this flag to false if you don't want to hide the search popup in some scenarios. For example, JIDE ComboBoxShrinkSearchableSupport will set this flag to false temporarily when it tries to shrink the list.

        Returns:
        true if the search popup is hidden on event. Otherwise false.
      • setHideSearchPopupOnEvent

        public void setHideSearchPopupOnEvent​(boolean hideSearchPopupOnEvent)
        Set the flag indicating if the search popup should be hidden on the component's event.
        Parameters:
        hideSearchPopupOnEvent - the flag
        See Also:
        isHideSearchPopupOnEvent()
      • hidePopup

        public void hidePopup()
        Hides the popup.
      • setSearchableProvider

        public void setSearchableProvider​(SearchableProvider searchableProvider)
      • installListeners

        public void installListeners()
        Installs necessary listeners to the component. This method will be called automatically when Searchable is created.
      • createComponentListener

        protected java.awt.event.ComponentListener createComponentListener()
        Creates a component listener that updates the popup when component is hidden, moved or resized.
        Returns:
        a ComponentListener.
      • createKeyListener

        protected java.awt.event.KeyListener createKeyListener()
        Creates the KeyListener and listen to key typed in the component.
        Returns:
        the KeyListener.
      • createFocusListener

        protected java.awt.event.FocusListener createFocusListener()
        Creates a FocusListener. We use it to hide the popup when the component loses focus.
        Returns:
        a FocusListener.
      • uninstallListeners

        public void uninstallListeners()
        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.
      • addPropertyChangeListener

        public void addPropertyChangeListener​(java.beans.PropertyChangeListener propertychangelistener)
        Adds the property change listener. The only property change event that will be fired is the "searchText" property which will be fired when user types in a different search text in the popup.
        Parameters:
        propertychangelistener - the listener
      • removePropertyChangeListener

        public void removePropertyChangeListener​(java.beans.PropertyChangeListener propertychangelistener)
        Removes the property change listener.
        Parameters:
        propertychangelistener - the listener
      • firePropertyChangeEvent

        public void firePropertyChangeEvent​(java.lang.String searchingText)
      • searchingTextEmpty

        protected void searchingTextEmpty()
        Actions to take on searching text empty scenario
      • compare

        protected boolean compare​(java.lang.Object element,
                                  java.lang.String searchingText)
        Checks if the element matches the searching text.
        Parameters:
        element - the element to be checked
        searchingText - the searching text
        Returns:
        true if matches.
      • compare

        protected boolean compare​(java.lang.String text,
                                  java.lang.String searchingText)
        Checks if the element string matches the searching text. Different from compare(Object, String), this method is after the element has been converted to string using convertElementToString(Object).
        Parameters:
        text - the text to be checked
        searchingText - the searching text
        Returns:
        true if matches.
      • getCursor

        public int getCursor()
        Gets the cursor which is the index of current location when searching. The value will be used in findNext and findPrevious.
        Returns:
        the current position of the cursor.
      • setCursor

        public void setCursor​(int cursor)
        Sets the cursor which is the index of current location when searching. The value will be used in findNext and findPrevious.
        Parameters:
        cursor - the new position of the cursor.
      • setCursor

        public void setCursor​(int cursor,
                              boolean incremental)
        Sets the cursor which is the index of current location when searching. The value will be used in findNext and findPrevious. We will call this method automatically inside this class. However, if you ever call setSelectedIndex(int, boolean) method from your code, you should call this method with the same parameters.
        Parameters:
        cursor - the new position of the cursor.
        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.
      • highlightAll

        protected void highlightAll()
        Highlight all matching cases in the target.

        In default implementation, it will just search all texts in the target to highlight all. If you have a really huge text to search, you may want to override this method to have a lazy behavior on visible areas only.

      • cancelHighlightAll

        protected void cancelHighlightAll()
        Cancel highlight all.

        By default, it does nothing. However, if you want to override highlightAll(), you may want to override this method to notify your Searchable that the highlightAll button is to be released.

      • select

        protected void select​(int index,
                              java.lang.String searchingText)
        Select the index for the searching text.
        Parameters:
        index - the start offset
        searchingText - the searching text presented in the searchable event to be fired here.
      • findNext

        public int findNext​(java.lang.String s)
        Finds the next matching index from the cursor.
        Parameters:
        s - the searching text
        Returns:
        the next index that the element matches the searching text.
      • getCurrentIndex

        protected int getCurrentIndex()
      • findPrevious

        public int findPrevious​(java.lang.String s)
        Finds the previous matching index from the cursor.
        Parameters:
        s - the searching text
        Returns:
        the previous index that the element matches the searching text.
      • findFromCursor

        public int findFromCursor​(java.lang.String s)
        Finds the next matching index from the cursor. If it reaches the end, it will restart from the beginning. However is the reverseOrder flag is true, it will finds the previous matching index from the cursor. If it reaches the beginning, it will restart from the end.
        Parameters:
        s - the searching text
        Returns:
        the next index that the element matches the searching text.
      • reverseFindFromCursor

        public int reverseFindFromCursor​(java.lang.String s)
        Finds the previous matching index from the cursor. If it reaches the beginning, it will restart from the end.
        Parameters:
        s - the searching text
        Returns:
        the next index that the element matches the searching text.
      • findFirst

        public int findFirst​(java.lang.String s)
        Finds the first element that matches the searching text.
        Parameters:
        s - the searching text
        Returns:
        the first element that matches with the searching text.
      • findLast

        public int findLast​(java.lang.String s)
        Finds the last element that matches the searching text.
        Parameters:
        s - the searching text
        Returns:
        the last element that matches the searching text.
      • keyTypedOrPressed

        protected void keyTypedOrPressed​(java.awt.event.KeyEvent e)
        This method is called when a key is typed or pressed.
        Parameters:
        e - the KeyEvent.
      • showPopup

        public void showPopup​(java.lang.String searchingText)
        Shows the search popup. By default, the search popup will be visible automatically when user types in the first key (in the case of JList, JTree, JTable) or types in designated keystroke (in the case of JTextComponent). So this method is only used when you want to show the popup manually.
        Parameters:
        searchingText - the searching text
      • createSearchPopup

        protected Searchable.SearchPopup createSearchPopup​(java.lang.String searchingText)
        Creates the popup to hold the searching text.
        Parameters:
        searchingText - the searching text
        Returns:
        the searching popup.
      • getSearchingText

        public java.lang.String getSearchingText()
        Gets the searching text.
        Returns:
        the searching text.
      • isFindFirstKey

        protected boolean isFindFirstKey​(java.awt.event.KeyEvent e)
        Checks if the key is used as a key to find the first occurrence.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to find the firstoccurrencee. By default, home key is used.
      • isFindLastKey

        protected boolean isFindLastKey​(java.awt.event.KeyEvent e)
        Checks if the key is used as a key to find the last occurrence.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to find the last occurrence. By default, end key is used.
      • isFindPreviousKey

        protected boolean isFindPreviousKey​(java.awt.event.KeyEvent e)
        Checks if the key is used as a key to find the previous occurrence.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to find the previous occurrence. By default, up arrow key is used.
      • isFindNextKey

        protected boolean isFindNextKey​(java.awt.event.KeyEvent e)
        Checks if the key is used as a key to find the next occurrence.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to find the next occurrence. By default, down arrow key is used.
      • isNavigationKey

        protected boolean isNavigationKey​(java.awt.event.KeyEvent e)
        Checks if the key is used as a navigation key. Navigation keys are keys which are used to navigate to other occurrences of the searching string.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a navigation key.
      • isActivateKey

        protected boolean isActivateKey​(java.awt.event.KeyEvent e)
        Checks if the key in KeyEvent should activate the search popup.
        Parameters:
        e - the key event
        Returns:
        true if the keyChar is visible except space and tab.
      • isDeactivateKey

        protected boolean isDeactivateKey​(java.awt.event.KeyEvent e)
        Checks if the key in KeyEvent should hide the search popup. If this method return true and the key is not used for navigation purpose (isNavigationKey(java.awt.event.KeyEvent) return false), the popup will be hidden.
        Parameters:
        e - the key event
        Returns:
        true if the keyCode in the KeyEvent is escape key, enter key, or any of the arrow keys such as page up, page down, home, end, left, right, up and down.
      • isSelectAllKey

        protected boolean isSelectAllKey​(java.awt.event.KeyEvent e)
        Checks if the key will trigger selecting all.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to trigger selecting all.
      • isIncrementalSelectKey

        protected boolean isIncrementalSelectKey​(java.awt.event.KeyEvent e)
        Checks if the key will trigger incremental selection.
        Parameters:
        e - the key event
        Returns:
        true if the key in KeyEvent is a key to trigger incremental selection. By default, ctrl down key is used.
      • getMismatchForeground

        public java.awt.Color getMismatchForeground()
        Gets the foreground color when the searching text doesn't match with any of the elements in the component.
        Returns:
        the foreground color for mismatch. If you never call setMismatchForeground(java.awt.Color). red color will be used.
      • setMismatchForeground

        public void setMismatchForeground​(java.awt.Color mismatchForeground)
        Sets the foreground for mismatch.
        Parameters:
        mismatchForeground - mismatch forground
      • isCaseSensitive

        public boolean isCaseSensitive()
        Checks if the case is sensitive during searching.
        Returns:
        true if the searching is case sensitive.
      • setCaseSensitive

        public void setCaseSensitive​(boolean caseSensitive)
        Sets the case sensitive flag. By default, it's false meaning it's a case insensitive search.
        Parameters:
        caseSensitive - the flag if searching is case sensitive
      • getSearchingDelay

        public int getSearchingDelay()
        If it returns a positive number, it will wait for that many ms before doing the search. When the searching is complex, this flag will be useful to make the searching efficient. In the other words, if user types in several keys very quickly, there will be only one search. If it returns 0 or negative number, each key will generate a search.
        Returns:
        the number of ms delay before searching starts.
      • setSearchingDelay

        public void setSearchingDelay​(int searchingDelay)
        If this flag is set to a positive number, it will wait for that many ms before doing the search. When the searching is complex, this flag will be useful to make the searching efficient. In the other words, if user types in several keys very quickly, there will be only one search. If this flag is set to 0 , each key will generate a search with no delay. If this flag is set to a negative number, there are different behaviors. SearchableBar will not generate any search while typing, but others will generate a search with no delay as well as it is set to 0.
        Parameters:
        searchingDelay - the number of ms delay before searching start.
      • isRepeats

        public boolean isRepeats()
        Checks if restart from the beginning when searching reaches the end or restart from the end when reaches beginning. Default is false.
        Returns:
        true or false.
      • setRepeats

        public void setRepeats​(boolean repeats)
        Sets the repeat flag. By default, it's false meaning it will stop searching when reaching the end or reaching the beginning.
        Parameters:
        repeats - the repeat flag
      • getForeground

        public java.awt.Color getForeground()
        Gets the foreground color used inn the search popup.
        Returns:
        the foreground. By default it will use the foreground of tooltip.
      • setForeground

        public void setForeground​(java.awt.Color foreground)
        Sets the foreground color used by popup.
        Parameters:
        foreground - the foreground
      • getBackground

        public java.awt.Color getBackground()
        Gets the background color used inn the search popup.
        Returns:
        the background. By default it will use the background of tooltip.
      • setBackground

        public void setBackground​(java.awt.Color background)
        Sets the background color used by popup.
        Parameters:
        background - the background
      • isWildcardEnabled

        public boolean isWildcardEnabled()
        Checks if it supports wildcard in searching text. By default it is true which means user can type in "*" or "?" to match with any characters or any character. If it's false, it will treat "*" or "?" as a regular character.
        Returns:
        true if it supports wildcard.
      • setWildcardEnabled

        public void setWildcardEnabled​(boolean wildcardEnabled)
        Enable or disable the usage of wildcard.
        Parameters:
        wildcardEnabled - the flag if wildcard is enabled
        See Also:
        isWildcardEnabled()
      • setWildcardSupport

        public void setWildcardSupport​(WildcardSupport wildcardSupport)
        Sets the WildcardSupport. This class allows you to define what wildcards to use and how to convert the wildcard strings to a regular expression string which is eventually used to search.
        Parameters:
        wildcardSupport - the new WildCardSupport.
      • getSearchLabel

        public java.lang.String getSearchLabel()
        Gets the current text that appears in the search popup. By default it is "Search for: ".
        Returns:
        the text that appears in the search popup.
      • setSearchLabel

        public void setSearchLabel​(java.lang.String searchLabel)
        Sets the text that appears in the search popup.
        Parameters:
        searchLabel - the search label
      • addSearchableListener

        public void addSearchableListener​(SearchableListener l)
        Adds the specified listener to receive searchable events from this searchable.
        Parameters:
        l - the searchable listener
      • removeSearchableListener

        public void removeSearchableListener​(SearchableListener l)
        Removes the specified searchable listener so that it no longer receives searchable events.
        Parameters:
        l - the searchable listener
      • isSearchableListenerInstalled

        public boolean isSearchableListenerInstalled​(SearchableListener l)
        Returns if a given listener is already installed.
        Parameters:
        l - the listener
        Returns:
        true if the listener is already installed. Otherwise false.
        Since:
        3.2.3
      • fireSearchableEvent

        protected void fireSearchableEvent​(SearchableEvent e)
        Fires a searchable event.
        Parameters:
        e - the event
      • getComponent

        public java.awt.Component getComponent()
        Gets the actual component which installed this Searchable.
        Returns:
        the actual component which installed this Searchable.
      • getPopupLocation

        public int getPopupLocation()
        Gets the popup location. It could be either SwingConstants.TOP or SwingConstants.BOTTOM.
        Returns:
        the popup location.
      • setPopupLocation

        public void setPopupLocation​(int popupLocation)
        Sets the popup location.
        Parameters:
        popupLocation - the popup location. The valid values are either SwingConstants.TOP or SwingConstants.BOTTOM.
      • isReverseOrder

        public boolean isReverseOrder()
        Checks the searching order. By default the searchable starts searching from top to bottom. If this flag is true, it searches from bottom to top.
        Returns:
        the reverseOrder flag.
      • setReverseOrder

        public void setReverseOrder​(boolean reverseOrder)
        Sets the searching order. By default the searchable starts searching from top to bottom. If this flag is true, it searches from bottom to top.
        Parameters:
        reverseOrder - the flag if searching from top to bottom or from bottom to top
      • getResourceString

        protected java.lang.String getResourceString​(java.lang.String key)
        Gets the localized string from resource bundle. Subclass can override it to provide its own string. Available keys are defined in swing.properties that begin with "Searchable.".
        Parameters:
        key - the resource string key
        Returns:
        the localized string.
      • isPopupVisible

        public boolean isPopupVisible()
        Check if the searchable popup is visible.
        Returns:
        true if visible. Otherwise, false.
      • isHeavyweightComponentEnabled

        public boolean isHeavyweightComponentEnabled()
      • setHeavyweightComponentEnabled

        public void setHeavyweightComponentEnabled​(boolean heavyweightComponentEnabled)
      • getPopupLocationRelativeTo

        public java.awt.Component getPopupLocationRelativeTo()
        Gets the component that the location of the popup relative to.
        Returns:
        the component that the location of the popup relative to.
      • setPopupLocationRelativeTo

        public void setPopupLocationRelativeTo​(java.awt.Component popupLocationRelativeTo)
        Sets the location of the popup relative to the specified component. Then based on the value of getPopupLocation(). If you never set, we will use the searchable component or its scroll pane (if exists) as the popupLocationRelativeTo component.
        Parameters:
        popupLocationRelativeTo - the relative component
      • isFromStart

        public boolean isFromStart()
        This is a property of how to compare searching text with the data. If it is true, it will use String.startsWith(String) to do the comparison. Otherwise, it will use String.indexOf(String) to do the comparison.
        Returns:
        true or false.
      • setFromStart

        public void setFromStart​(boolean fromStart)
        Sets the fromStart property.
        Parameters:
        fromStart - true if the comparison matches from the start of the text only. Otherwise false. The difference is if true, it will use String's startWith method to match. If false, it will use indedxOf method.
      • getSearchable

        public static Searchable getSearchable​(javax.swing.JComponent component)
        Gets the Searchable installed on the component. Null is no Searchable was installed.
        Parameters:
        component - the component
        Returns:
        the Searchable installed. Null is no Searchable was installed.
      • isProcessModelChangeEvent

        public boolean isProcessModelChangeEvent()
        Get the flag if we should process model change event.

        By default, the value is true, which means the model change event should be processed.

        In ListShrinkSearchableSupport case, since we will fire this event while applying filters. This flag will be switched to false before we fire the event and set it back to true.

        In normal case, please do not set this flag.

        Returns:
        true if we should process model change event. Otherwise false.
      • setProcessModelChangeEvent

        public void setProcessModelChangeEvent​(boolean processModelChangeEvent)
        Set the flag if we should process model change event.

        In normal case, please do not set this flag.

        Parameters:
        processModelChangeEvent - the flag
        See Also:
        isProcessModelChangeEvent()
      • getPopupTimeout

        public int getPopupTimeout()
        Gets the timeout for showing the popup.
        Returns:
        the popup timeout.
        See Also:
        setPopupTimeout(int)
      • setPopupTimeout

        public void setPopupTimeout​(int popupTimeout)
        Sets the timeout for showing the popup.

        By default, the timeout value is 0, which means no timeout. You could set it to a positive value to automatically hide the search popup after an idle time.

        Parameters:
        popupTimeout - the timeout in milliseconds
      • isCountMatch

        public boolean isCountMatch()
        Gets the flag indicating if the Searchable should count all matches for every search.
        Returns:
        true if should count all matches. Otherwise false.
        Since:
        3.5.2
        See Also:
        setCountMatch(boolean)
      • setCountMatch

        public void setCountMatch​(boolean countMatch)
        Sets the flag indicating if the Searchable should count all matches for every search.

        By default, the flag is false to keep performance high.

        Parameters:
        countMatch - the flag
        Since:
        3.5.2
      • findAll

        public java.util.List<java.lang.Integer> findAll​(java.lang.String s)
        findAll uses the Searchable to find all the element indices that match the searching string.
        Parameters:
        s - the searching string.
        Returns:
        the list of indices.
      • getElementAtAsString

        public java.lang.String getElementAtAsString​(int index)
        Gets the element at the specified index as string using convertElementToString(Object) method.
        Parameters:
        index - the index.
        Returns:
        the element at the index converted to string.
      • textChanged

        protected void textChanged​(java.lang.String text)
      • findFirstExactly

        public int findFirstExactly​(java.lang.String s)
        Finds the first element that matches the searching text exactly.
        Parameters:
        s - the searching text
        Returns:
        the first element that matches with the searching text.
        Since:
        3.6.1