jas2.util
Class JASEditableComboBox
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JComboBox
-
- jas2.util.JASEditableComboBox
-
- All Implemented Interfaces:
- java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.ListDataListener
public final class JASEditableComboBox extends javax.swing.JComboBox implements java.awt.event.KeyListenerThis class is ideal if you want to have a text field for user input but also have a list of recently selected items available to choose from. Some notes on using this class:- The default button will be clicked on the root pane of this
component if the value of the constructor parameter
clickDefaultistrue. This behaviour may be desirable because the default button is normally "clicked" when the user clicks on the "Enter" key, but this behaviour doesn't normally happen unless you add an ActionListener to the text field. - Calling
addActionListener(ActionListener)on this object causes the ActionListener to be added to the text field. - A KeyEvent is generated for every time a key is pressed in the field, and all listeners are notified. KeyEvents normally generated by the JComboBox will not be sent by this class; they are suppressed.
- A ChangeEvent is generated any time the text in the field changes. Therefore, if you want to update a button state (for example) each time the text changes, simply implement a ChangeListener and add it to the listener list for this object. Otherwise, you would have to add a KeyListener and an ItemListener, and it's better just to have one.
Basically, a ChangeEvent is a dual purpose event. One is sent to listeners both when a KeyEvent is generated from the text field, and one is sent when an ItemEvent is generated (which happens when an item is selected from the drop-down list). This means that somebody who wants to know when the actual text showing has changed only needs to implement ChangeListener instead of both ItemListener and KeyListener.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javax.swing.JComboBox
javax.swing.JComboBox.KeySelectionManager
-
-
Field Summary
-
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
Constructor Summary
Constructors Constructor and Description JASEditableComboBox(java.lang.String[] dropDownItems, boolean clickDefault)Creates a JASEditableComboBox with the given list of drop-down items.JASEditableComboBox(java.lang.String key, int nItems, boolean clickDefault)Creates a JASEditableComboBox with a dropDown list that will be stored in the UserProperties object for the Application.JASEditableComboBox(java.lang.String key, java.lang.String lengthKey, boolean clickDefault)Creates a JASEditableComboBox with a dropDown list that will be stored in the UserProperties object for the Application.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddActionListener(java.awt.event.ActionListener l)Adds an the given ActionListener to the text field.voidaddChangeListener(javax.swing.event.ChangeListener l)Change listeners will be notified any time the text visible in the text field changes.voidaddKeyListener(java.awt.event.KeyListener kl)Adds a key listener to the text field, not to the JComboBox.java.awt.DimensiongetMaximumSize()Encorporates the maximum width if it has been set.java.awt.DimensiongetMinimumSize()Encorporates the minimum width if it has been set.java.awt.DimensiongetPreferredSize()Encorporates the minimum and maximum widths if they have been set.java.lang.StringgetText()Returns the text currently showing in the text field.voidkeyPressed(java.awt.event.KeyEvent e)This method is public as an implementation side effect; do not call.voidkeyReleased(java.awt.event.KeyEvent e)This method is public as an implementation side effect; do not call.voidkeyTyped(java.awt.event.KeyEvent e)This method is public as an implementation side effect; do not call.voidrequestFocus()Requests focus for the text field of the box.voidsaveState()If a key was supplied to the constructor, the drop-down list will be updated to include the currently selected item.voidsetMaxWidth(int maxWidth)Set the maximum width of the box.voidsetMinWidth(int minWidth)Set the minimum width of the box.voidsetText(java.lang.String s)Sets the text showing in the text field.-
Methods inherited from class javax.swing.JComboBox
actionPerformed, addItem, addItemListener, addPopupMenuListener, configureEditor, contentsChanged, firePopupMenuCanceled, firePopupMenuWillBecomeInvisible, firePopupMenuWillBecomeVisible, getAccessibleContext, getAction, getActionCommand, getActionListeners, getEditor, getItemAt, getItemCount, getItemListeners, getKeySelectionManager, getMaximumRowCount, getModel, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedItem, getSelectedObjects, getUI, getUIClassID, hidePopup, insertItemAt, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, processKeyEvent, removeActionListener, removeAllItems, removeItem, removeItemAt, removeItemListener, removePopupMenuListener, selectWithKeyChar, setAction, setActionCommand, setEditable, setEditor, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, setSelectedItem, setUI, showPopup, updateUI
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Constructor Detail
-
JASEditableComboBox
public JASEditableComboBox(java.lang.String[] dropDownItems, boolean clickDefault)Creates a JASEditableComboBox with the given list of drop-down items. You are responsible for keeping and providing an updated list.- Parameters:
dropDownItems- the items to show in the drop-down list (can safely benull)clickDefault- whether the default button should be clicked when Enter is pressed- See Also:
JRootPane.defaultButton
-
JASEditableComboBox
public JASEditableComboBox(java.lang.String key, int nItems, boolean clickDefault)Creates a JASEditableComboBox with a dropDown list that will be stored in the UserProperties object for the Application. Invoke thesaveState()method to have the list be updated to include the selected item.- Parameters:
key- the key that the drop-down items will be stored bynItems- the maximum number of items that will be stored on the drop-down listclickDefault- whether the default button should be clicked when Enter is pressed- See Also:
UserProperties,JRootPane.defaultButton
-
JASEditableComboBox
public JASEditableComboBox(java.lang.String key, java.lang.String lengthKey, boolean clickDefault)Creates a JASEditableComboBox with a dropDown list that will be stored in the UserProperties object for the Application. Invoke thesaveState()method to have the list be updated to include the selected item.- Parameters:
key- the key that the drop-down items will be stored bylengthKey- the key that maps to the maximum number of items that will be stored on the drop-down listclickDefault- whether the default button should be clicked when Enter is pressed- See Also:
UserProperties,JRootPane.defaultButton
-
-
Method Detail
-
saveState
public void saveState()
If a key was supplied to the constructor, the drop-down list will be updated to include the currently selected item.
-
getText
public java.lang.String getText()
Returns the text currently showing in the text field.
-
setText
public void setText(java.lang.String s)
Sets the text showing in the text field.
-
addKeyListener
public void addKeyListener(java.awt.event.KeyListener kl)
Adds a key listener to the text field, not to the JComboBox.- Overrides:
addKeyListenerin classjava.awt.Component
-
keyReleased
public final void keyReleased(java.awt.event.KeyEvent e)
This method is public as an implementation side effect; do not call.- Specified by:
keyReleasedin interfacejava.awt.event.KeyListener
-
keyPressed
public final void keyPressed(java.awt.event.KeyEvent e)
This method is public as an implementation side effect; do not call.- Specified by:
keyPressedin interfacejava.awt.event.KeyListener
-
addChangeListener
public void addChangeListener(javax.swing.event.ChangeListener l)
Change listeners will be notified any time the text visible in the text field changes. This is equivalent to having a KeyListener and an ItemListener.- Parameters:
l- the ChangeListener to add
-
addActionListener
public void addActionListener(java.awt.event.ActionListener l)
Adds an the given ActionListener to the text field.- Overrides:
addActionListenerin classjavax.swing.JComboBox
-
requestFocus
public final void requestFocus()
Requests focus for the text field of the box.- Overrides:
requestFocusin classjavax.swing.JComponent
-
keyTyped
public final void keyTyped(java.awt.event.KeyEvent e)
This method is public as an implementation side effect; do not call.- Specified by:
keyTypedin interfacejava.awt.event.KeyListener
-
setMaxWidth
public final void setMaxWidth(int maxWidth)
Set the maximum width of the box. The default is used if this method is not called.- Parameters:
maxWidth- give a number in pixels, or-1to use default
-
setMinWidth
public final void setMinWidth(int minWidth)
Set the minimum width of the box. The default is used if this method is not called.- Parameters:
minWidth- give a number in pixels, or-1to use default
-
getMaximumSize
public final java.awt.Dimension getMaximumSize()
Encorporates the maximum width if it has been set.- Overrides:
getMaximumSizein classjavax.swing.JComponent- See Also:
setMaxWidth(int)
-
getMinimumSize
public final java.awt.Dimension getMinimumSize()
Encorporates the minimum width if it has been set.- Overrides:
getMinimumSizein classjavax.swing.JComponent- See Also:
setMinWidth(int)
-
getPreferredSize
public final java.awt.Dimension getPreferredSize()
Encorporates the minimum and maximum widths if they have been set.- Overrides:
getPreferredSizein classjavax.swing.JComponent- See Also:
setMinWidth(int),setMaxWidth(int)
-
-
DataMelt 3.0 © DataMelt by jWork.ORG