public class JXSearchField extends JXTextField
JXSearchField.LayoutStyle
.
JXSearchField supports two different search modes: JXSearchField.SearchMode.INSTANT
and JXSearchField.SearchMode.REGULAR
.
A search can be performed by registering an ActionListener
. The
ActionEvent
s command property contains the text to search for. The
search should be cancelled, when the command text is empty or null.RecentSearches
,
Serialized FormModifier and Type | Class and Description |
---|---|
(package private) class |
JXSearchField.ClearAction
Invoked when the the cancel button or the 'Esc' key is pressed.
|
class |
JXSearchField.FindAction
Invoked when the find button is pressed.
|
static class |
JXSearchField.LayoutStyle
Defines, how the find and cancel button are layouted.
|
static class |
JXSearchField.SearchMode
Defines when action events are posted.
|
JTextField.AccessibleJTextField
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
private static KeyStroke |
CANCEL_KEY
The key used to invoke the cancel action.
|
private JButton |
cancelButton |
private static int |
DEFAULT_INSTANT_SEARCH_DELAY
The default instant search delay.
|
private JButton |
findButton |
private int |
instantSearchDelay |
private Timer |
instantSearchTimer |
private JXSearchField.LayoutStyle |
layoutStyle |
private boolean |
layoutStyleSet |
private JButton |
popupButton |
private boolean |
promptFontStyleSet |
private RecentSearches |
recentSearches |
private String |
recentSearchesSaveKey |
private JXSearchField.SearchMode |
searchMode |
private boolean |
useSeperatePopupButton |
private boolean |
useSeperatePopupButtonSet |
notifyAction
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JXSearchField()
Creates a new search field with a default prompt.
|
JXSearchField(String prompt)
Creates a new search field with the given prompt and
JXSearchField.SearchMode.INSTANT . |
Modifier and Type | Method and Description |
---|---|
protected JButton |
createCancelButton()
Creates and returns the cancel button.
|
protected JButton |
createFindButton()
Creates and returns the find button.
|
protected JButton |
createPopupButton()
Creates and returns the popup button.
|
void |
customSetUIProperty(String propertyName,
Object value)
Hack to enable the UI delegate to set default values depending on the
current Look and Feel, without overriding custom values.
|
void |
customSetUIProperty(String propertyName,
Object value,
boolean override)
Hack to enable the UI delegate to set default values depending on the
current Look and Feel, without overriding custom values.
|
ActionListener |
getCancelAction()
Returns the cancel action, or an instance of
JXSearchField.ClearAction , if
none has been set. |
JButton |
getCancelButton()
Returns the cancel button.
|
ActionListener |
getFindAction()
Returns the action that is invoked when the enter key is pressed or the
find button is clicked.
|
JButton |
getFindButton()
Returns the find button.
|
JPopupMenu |
getFindPopupMenu()
Returns the find popup menu.
|
int |
getInstantSearchDelay()
Get the instant search delay in milliseconds.
|
Timer |
getInstantSearchTimer()
Returns the
Timer used to delay the firing of action events in
instant search mode when the user enters text. |
JXSearchField.LayoutStyle |
getLayoutStyle()
Get the current
JXSearchField.LayoutStyle . |
JButton |
getPopupButton()
Returns the popup button.
|
RecentSearches |
getRecentSearches()
TODO
|
String |
getRecentSearchesSaveKey()
Returns the key used to persist recent searches.
|
JXSearchField.SearchMode |
getSearchMode()
Returns the current
JXSearchField.SearchMode . |
boolean |
hasFocus()
Returns
true if this search field is the focus owner or
the find popup menu is visible. |
boolean |
isInstantSearchMode()
|
boolean |
isMacLayoutStyle()
|
boolean |
isManagingRecentSearches()
TODO
|
boolean |
isRegularSearchMode()
|
boolean |
isUseNativeSearchFieldIfPossible() |
boolean |
isUseSeperatePopupButton()
Returns
true if the popup button should be visible and
used for displaying the find popup menu. |
private boolean |
isValidRecentSearchesKey(String key) |
boolean |
isVistaLayoutStyle()
|
void |
postActionEvent()
Overriden to prevent any delayed
ActionEvent s from being sent
after posting this action. |
void |
setCancelAction(ActionListener cancelAction)
Sets the action that is invoked, when the user presses the 'Esc' key or
clicks the cancel button.
|
void |
setEditable(boolean b)
Updates the cancel, find and popup buttons enabled state in addition to
setting the search fields editable state.
|
void |
setEnabled(boolean enabled)
Updates the cancel, find and popup buttons enabled state in addition to
setting the search fields enabled state.
|
void |
setFindAction(ActionListener findAction)
Sets the action that is invoked when the enter key is pressed or the find
button is clicked.
|
void |
setFindPopupMenu(JPopupMenu findPopupMenu)
Sets the popup menu that will be displayed when the popup button is
clicked.
|
void |
setInstantSearchDelay(int instantSearchDelay)
Set the instant search delay in milliseconds.
|
void |
setLayoutStyle(JXSearchField.LayoutStyle layoutStyle)
Set the current
JXSearchField.LayoutStyle . |
void |
setMargin(Insets m)
Set the margin space around the search field's text.
|
void |
setPromptFontStyle(Integer fontStyle)
Hack to enable the UI delegate to set default values depending on the
current Look and Feel, without overriding custom values.
|
void |
setRecentSearchesSaveKey(String recentSearchesSaveKey)
Installs and manages a recent searches popup menu as the find popup menu,
if
recentSearchesSaveKey is not null. |
void |
setSearchMode(JXSearchField.SearchMode searchMode)
Sets the current search mode.
|
void |
setUseNativeSearchFieldIfPossible(boolean useNativeSearchFieldIfPossible) |
void |
setUseSeperatePopupButton(boolean useSeperatePopupButton)
Set if the popup button should be used for displaying the find popup
menu.
|
protected void |
updateButtonState()
Enables the cancel action if this search field is editable and enabled,
otherwise it will be disabled.
|
void |
updateUI()
Overriden to also update the find popup menu if set.
|
addBuddy, addGap, getBuddies, getFocusBehavior, getOuterMargin, getPrompt, getPromptBackground, getPromptFontStyle, getPromptForeground, removeAllBuddies, setFocusBehavior, setOuterMargin, setPrompt, setPromptBackround, setPromptForeground
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, paramString, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setFocusAccelerator, setHighlighter, setKeymap, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, viewToModel, write
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
private static final int DEFAULT_INSTANT_SEARCH_DELAY
private static final KeyStroke CANCEL_KEY
private JButton findButton
private JButton cancelButton
private JButton popupButton
private JXSearchField.LayoutStyle layoutStyle
private JXSearchField.SearchMode searchMode
private boolean useSeperatePopupButton
private boolean useSeperatePopupButtonSet
private boolean layoutStyleSet
private int instantSearchDelay
private boolean promptFontStyleSet
private Timer instantSearchTimer
private String recentSearchesSaveKey
private RecentSearches recentSearches
public JXSearchField()
public JXSearchField(String prompt)
JXSearchField.SearchMode.INSTANT
.prompt
- public JXSearchField.SearchMode getSearchMode()
JXSearchField.SearchMode
.JXSearchField.SearchMode
.public boolean isInstantSearchMode()
true
if the current JXSearchField.SearchMode
is
JXSearchField.SearchMode.INSTANT
public boolean isRegularSearchMode()
true
if the current JXSearchField.SearchMode
is
JXSearchField.SearchMode.REGULAR
public void setSearchMode(JXSearchField.SearchMode searchMode)
JXSearchField.SearchMode
for a description of
the different search modes.searchMode
- JXSearchField.SearchMode.INSTANT
or JXSearchField.SearchMode.REGULAR
public int getInstantSearchDelay()
#setInstantSearchDelay(int)}
public void setInstantSearchDelay(int instantSearchDelay)
JXSearchField.SearchMode.INSTANT
, when the user changes the text, an action
event will be fired after the specified instant search delay.
It is recommended to use a instant search delay to avoid the firing of
unnecessary events. For example when the user replaces the whole text
with a different text the search fields underlying Document
typically fires 2 document events. The first one, because the old text is
removed and the second one because the new text is inserted. If the
instant search delay is 0, this would result in 2 action events being
fired. When a instant search delay is used, the first document event
typically is ignored, because the second one is fired before the delay is
over, which results in a correct behavior because only the last and only
relevant event will be delivered.instantSearchDelay
- public JXSearchField.LayoutStyle getLayoutStyle()
JXSearchField.LayoutStyle
.public boolean isVistaLayoutStyle()
public boolean isMacLayoutStyle()
public void setLayoutStyle(JXSearchField.LayoutStyle layoutStyle)
JXSearchField.LayoutStyle
. See JXSearchField.LayoutStyle
for a
description of how this affects layout and behavior of the search field.layoutStyle
- JXSearchField.LayoutStyle.MAC
or JXSearchField.LayoutStyle.VISTA
public void setMargin(Insets m)
setMargin
in class JTextComponent
JTextComponent.setMargin(java.awt.Insets)
public final ActionListener getCancelAction()
JXSearchField.ClearAction
, if
none has been set.public final void setCancelAction(ActionListener cancelAction)
cancelAction
- public final JButton getCancelButton()
createCancelButton()
to create the cancel button and
registers an ActionListener
that delegates actions to the
ActionListener
returned by getCancelAction()
, if
needed.protected JButton createCancelButton()
getCancelButton()
public final ActionListener getFindAction()
JXSearchField.FindAction
will be returned.public final void setFindAction(ActionListener findAction)
public final JButton getFindButton()
createFindButton()
to create the find button and registers
an ActionListener
that delegates actions to the
ActionListener
returned by getFindAction()
, if needed.protected JButton createFindButton()
#getSearchAction()
.
Override to use a custom find button.getFindButton()
public final JButton getPopupButton()
isUseSeperatePopupButton()
returns true
. Otherwise the popup menu will be displayed
when the find button is clicked.protected JButton createPopupButton()
getPopupButton()
public boolean isUseSeperatePopupButton()
true
if the popup button should be visible and
used for displaying the find popup menu. Otherwise, the find popup menu
will be displayed when the find button is clicked.true
if the popup button should be usedpublic void setUseSeperatePopupButton(boolean useSeperatePopupButton)
useSeperatePopupButton
- public boolean isUseNativeSearchFieldIfPossible()
public void setUseNativeSearchFieldIfPossible(boolean useNativeSearchFieldIfPossible)
public void setEditable(boolean b)
setEditable
in class JTextComponent
updateButtonState()
,
JTextComponent.setEditable(boolean)
public void setEnabled(boolean enabled)
setEnabled
in class JComponent
updateButtonState()
,
JComponent.setEnabled(boolean)
protected void updateButtonState()
public void setFindPopupMenu(JPopupMenu findPopupMenu)
isUseSeperatePopupButton()
returns false
, the
popup button will be displayed instead of the find button. Otherwise the
popup button will be displayed in addition to the find button.
The find popup menu is managed using NativeSearchFieldSupport
to
achieve compatibility with the native search field support provided by
the Mac Look And Feel since Mac OS 10.5.
If a recent searches save key has been set and therefore a recent
searches popup menu is installed, this method does nothing. You must
first remove the recent searches save key, by calling
setRecentSearchesSaveKey(String)
with a null
parameter.findPopupMenu
- the popup menu, which will be displayed when the popup button
is clickedsetRecentSearchesSaveKey(String)
,
RecentSearches
public JPopupMenu getFindPopupMenu()
setFindPopupMenu(JPopupMenu)
public final boolean isManagingRecentSearches()
private boolean isValidRecentSearchesKey(String key)
public String getRecentSearchesSaveKey()
setRecentSearchesSaveKey(String)
public void setRecentSearchesSaveKey(String recentSearchesSaveKey)
recentSearchesSaveKey
is not null. Otherwise, removes
the popup menu and stops managing recent searches.recentSearchesSaveKey
- this key is used to persist the recent searches.setFindAction(ActionListener)
,
isManagingRecentSearches()
,
RecentSearches
public RecentSearches getRecentSearches()
public Timer getInstantSearchTimer()
Timer
used to delay the firing of action events in
instant search mode when the user enters text.
This timer calls postActionEvent()
.Timer
used to delay the firing of action eventspublic boolean hasFocus()
true
if this search field is the focus owner or
the find popup menu is visible.
This is a hack to make the search field paint the focus indicator in Mac
OS X Aqua when the find popup menu is visible.public void updateUI()
updateUI
in class JTextComponent
public void setPromptFontStyle(Integer fontStyle)
setPromptFontStyle
in class JXTextField
PromptSupport.setFontStyle(Integer, javax.swing.text.JTextComponent)
public void customSetUIProperty(String propertyName, Object value)
propertyName
- the name of the property to changevalue
- the new value of the propertypublic void customSetUIProperty(String propertyName, Object value, boolean override)
propertyName
- the name of the property to changevalue
- the new value of the propertyoverride
- override custom valuespublic void postActionEvent()
ActionEvent
s from being sent
after posting this action.
For example, if the current JXSearchField.SearchMode
is
JXSearchField.SearchMode.INSTANT
and the instant search delay is greater 0. The
user enters some text and presses enter. This method will be invoked
immediately because the users presses enter. However, this method would
be invoked after the instant search delay, if we would not prevent it
here.postActionEvent
in class JTextField
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses