public class TextComponentSearchable extends Searchable implements javax.swing.event.DocumentListener, java.beans.PropertyChangeListener
TextComponentSearchable is an concrete implementation of Searchable that enables the search
function in JTextComponent. It's very simple to use it. Assuming you have a JTextComponent, all you need to do is
to call
Now the JTextComponent will have the search function.
JTextComponent textComponent = ....;
TextComponentSearchable searchable = new TextComponentSearchable(textComponent);
JTextComponent textComponent = ....;
TextComponentSearchable searchable = new ListSearchable(textComponent) {
protected String convertElementToString(Object object) {
...
}
protected boolean isActivateKey(KeyEvent e) { // change to a different activation key
return (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_F &&
(KeyEvent.CTRL_MASK & e.getModifiers()) != 0);
}
};
Additional customization can be done on the base Searchable class such as background and foreground color,
keystrokes, case sensitivity. TextComponentSearchable also has a special attribute called highlightColor. You can
change it using setHighlightColor(java.awt.Color).
Due to the special case of JTextComponent, the searching doesn't support wild card '*' or '?' as in other
Searchables. The other difference is JTextComponent will keep the highlights after search popup hides. If you want to
hide the highlights, just press ESC again (the first ESC will hide popup; the second ESC will hide all highlights if
any).Searchable.DefaultSearchPopup, Searchable.SearchField, Searchable.SearchPopup_component, _componentListener, _focusListener, _keyListener, _matchCount, CLIENT_PROPERTY_SEARCHABLE, listenerList, PROPERTY_SEARCH_TEXT| Constructor and Description |
|---|
TextComponentSearchable(javax.swing.text.JTextComponent textComponent) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addHighlight(int index,
java.lang.String text,
boolean incremental)
Adds highlight to text component at specified index and text.
|
void |
changedUpdate(javax.swing.event.DocumentEvent e) |
protected java.lang.String |
convertElementToString(java.lang.Object object)
Converts the element in JTextComponent to string.
|
int |
findFirst(java.lang.String s)
Finds the first element that matches the searching text.
|
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.
|
protected java.lang.Object |
getElementAt(int index)
Gets the element at the specified index.
|
protected int |
getElementCount()
Gets the total element count in the component.
|
java.awt.Color |
getHighlightColor()
Gets the highlight color.
|
protected int |
getSelectedIndex()
Gets the selected index in the component.
|
void |
hidePopup()
Hides the popup.
|
void |
insertUpdate(javax.swing.event.DocumentEvent e) |
void |
installHighlightsRemover()
Installs the handler for ESC key to remove all highlights
|
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.
|
void |
propertyChange(java.beans.PropertyChangeEvent evt) |
protected void |
removeAllHighlights()
Removes all highlights from the text component.
|
void |
removeUpdate(javax.swing.event.DocumentEvent e) |
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
|
void |
setHighlightColor(java.awt.Color highlightColor)
Changes the highlight color.
|
protected void |
setSelectedIndex(int index,
boolean incremental)
Sets the selected index.
|
void |
uninstallHighlightsRemover()
Uninstalls the handler for ESC key to remove all highlights
|
void |
uninstallListeners()
Uninstall the listeners that installed before.
|
addPropertyChangeListener, addSearchableListener, adjustSelectedIndex, cancelHighlightAll, compare, compare, convertToString, createComponentListener, createFocusListener, createKeyListener, createSearchPopup, findAll, findFirstExactly, firePropertyChangeEvent, fireSearchableEvent, getBackground, getComponent, getCurrentIndex, getCursor, getElementAtAsString, getForeground, getMismatchForeground, getPopupLocation, getPopupLocationRelativeTo, getPopupTimeout, getResourceString, getSearchable, getSearchableListeners, getSearchableProvider, getSearchingDelay, getSearchingText, getSearchLabel, getWildcardSupport, highlightAll, isCaseSensitive, isCountMatch, isDeactivateKey, isFindFirstKey, isFindLastKey, isFindNextKey, isFindPreviousKey, isFromStart, isHeavyweightComponentEnabled, isHideSearchPopupOnEvent, isIncrementalSelectKey, isNavigationKey, isPopupVisible, isProcessModelChangeEvent, isRepeats, isReverseOrder, isSearchableListenerInstalled, isSelectAllKey, isWildcardEnabled, keyTypedOrPressed, removePropertyChangeListener, removeSearchableListener, select, setBackground, setCaseSensitive, setCountMatch, setCursor, setCursor, setForeground, setFromStart, setHeavyweightComponentEnabled, setHideSearchPopupOnEvent, setMismatchForeground, setPopupLocation, setPopupLocationRelativeTo, setPopupTimeout, setProcessModelChangeEvent, setRepeats, setReverseOrder, setSearchableProvider, setSearchingDelay, setSearchLabel, setWildcardEnabled, setWildcardSupport, showPopup, textChangedpublic TextComponentSearchable(javax.swing.text.JTextComponent textComponent)
public void uninstallHighlightsRemover()
public void installHighlightsRemover()
public void installListeners()
SearchableinstallListeners in class Searchablepublic void uninstallListeners()
SearchableuninstallListeners in class Searchableprotected void setSelectedIndex(int index,
boolean incremental)
SearchablesetSelectedIndex in class Searchableindex - the index to be selectedincremental - 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.protected void addHighlight(int index,
java.lang.String text,
boolean incremental)
throws javax.swing.text.BadLocationException
index - the index of the text to be highlightedtext - the text to be highlightedincremental - if this is an incremental adding highlightjavax.swing.text.BadLocationExceptionprotected void removeAllHighlights()
protected int getSelectedIndex()
SearchableHere 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).
getSelectedIndex in class Searchableprotected java.lang.Object getElementAt(int index)
SearchablegetElementAt in class Searchableindex - the indexprotected int getElementCount()
SearchablegetElementCount in class Searchableprotected java.lang.String convertElementToString(java.lang.Object object)
toString() of
whatever element that returned from list.getModel().getElementAt(i).convertElementToString in class Searchableobject - public void propertyChange(java.beans.PropertyChangeEvent evt)
propertyChange in interface java.beans.PropertyChangeListenerpublic void insertUpdate(javax.swing.event.DocumentEvent e)
insertUpdate in interface javax.swing.event.DocumentListenerpublic void removeUpdate(javax.swing.event.DocumentEvent e)
removeUpdate in interface javax.swing.event.DocumentListenerpublic void changedUpdate(javax.swing.event.DocumentEvent e)
changedUpdate in interface javax.swing.event.DocumentListenerprotected boolean isActivateKey(java.awt.event.KeyEvent e)
SearchableisActivateKey in class Searchablee - the key eventpublic java.awt.Color getHighlightColor()
public void setHighlightColor(java.awt.Color highlightColor)
highlightColor - public int findLast(java.lang.String s)
SearchablefindLast in class Searchables - the searching textpublic int findFirst(java.lang.String s)
SearchablefindFirst in class Searchables - the searching textpublic int findFromCursor(java.lang.String s)
SearchablefindFromCursor in class Searchables - the searching textpublic int reverseFindFromCursor(java.lang.String s)
SearchablereverseFindFromCursor in class Searchables - the searching textpublic int findNext(java.lang.String s)
SearchablefindNext in class Searchables - the searching textpublic int findPrevious(java.lang.String s)
SearchablefindPrevious in class Searchables - the searching textpublic void hidePopup()
SearchablehidePopup in class Searchableprotected void searchingTextEmpty()
SearchablesearchingTextEmpty in class Searchable