Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / JComboBox.java
blobcd30840a6aa841aea402c6a5e92b6f7dd9d2037e
1 /* JComboBox.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.ItemSelectable;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
46 import java.awt.event.KeyEvent;
47 import java.beans.PropertyChangeEvent;
48 import java.beans.PropertyChangeListener;
49 import java.util.Vector;
51 import javax.accessibility.Accessible;
52 import javax.accessibility.AccessibleAction;
53 import javax.accessibility.AccessibleContext;
54 import javax.accessibility.AccessibleRole;
55 import javax.accessibility.AccessibleSelection;
56 import javax.swing.event.ListDataEvent;
57 import javax.swing.event.ListDataListener;
58 import javax.swing.event.PopupMenuListener;
59 import javax.swing.event.PopupMenuEvent;
60 import javax.swing.plaf.ComboBoxUI;
62 /**
63 * A component that allows a user to select any item in its list and
64 * displays the selected item to the user. JComboBox also can show/hide a
65 * popup menu containing its list of item whenever the mouse is pressed
66 * over it.
68 * @author Andrew Selkirk
69 * @author Olga Rodimina
70 * @author Robert Schuster
72 public class JComboBox extends JComponent implements ItemSelectable,
73 ListDataListener,
74 ActionListener,
75 Accessible
78 private static final long serialVersionUID = 5654585963292734470L;
80 /**
81 * Classes implementing this interface are
82 * responsible for matching key characters typed by the user with combo
83 * box's items.
85 public static interface KeySelectionManager
87 int selectionForKey(char aKey, ComboBoxModel aModel);
90 /**
91 * Maximum number of rows that should be visible by default in the
92 * JComboBox's popup
94 private static final int DEFAULT_MAXIMUM_ROW_COUNT = 8;
96 /**
97 * Data model used by JComboBox to keep track of its list data and currently
98 * selected element in the list.
100 protected ComboBoxModel dataModel;
103 * Renderer renders(paints) every object in the combo box list in its
104 * associated list cell. This ListCellRenderer is used only when this
105 * JComboBox is uneditable.
107 protected ListCellRenderer renderer;
110 * Editor that is responsible for editing an object in a combo box list.
112 protected ComboBoxEditor editor;
115 * Number of rows that will be visible in the JComboBox's popup.
117 protected int maximumRowCount;
120 * This field indicates if textfield of this JComboBox is editable or not.
122 protected boolean isEditable;
125 * This field is reference to the current selection of the combo box.
127 protected Object selectedItemReminder;
130 * keySelectionManager
132 protected KeySelectionManager keySelectionManager;
135 * This actionCommand is used in ActionEvent that is fired to JComboBox's
136 * ActionListeneres.
138 protected String actionCommand;
141 * This property indicates if heavyweight popup or lightweight popup will be
142 * used to diplay JComboBox's elements.
144 protected boolean lightWeightPopupEnabled;
147 * The action taken when new item is selected in the JComboBox
149 private Action action;
152 * since 1.4 If this field is set then comboBox's display area for the
153 * selected item will be set by default to this value.
155 private Object prototypeDisplayValue;
158 * Constructs JComboBox object with specified data model for it.
159 * <p>Note that the JComboBox will not change the value that
160 * is preselected by your ComboBoxModel implementation.</p>
162 * @param model Data model that will be used by this JComboBox to keep track
163 * of its list of items.
165 public JComboBox(ComboBoxModel model)
167 setEditable(false);
168 setEnabled(true);
169 setMaximumRowCount(DEFAULT_MAXIMUM_ROW_COUNT);
170 setModel(model);
171 setActionCommand("comboBoxChanged");
173 lightWeightPopupEnabled = true;
174 isEditable = false;
176 updateUI();
180 * Constructs JComboBox with specified list of items.
182 * @param itemArray array containing list of items for this JComboBox
184 public JComboBox(Object[] itemArray)
186 this(new DefaultComboBoxModel(itemArray));
188 if (itemArray.length > 0)
189 setSelectedIndex(0);
193 * Constructs JComboBox object with specified list of items.
195 * @param itemVector vector containing list of items for this JComboBox.
197 public JComboBox(Vector itemVector)
199 this(new DefaultComboBoxModel(itemVector));
201 if (itemVector.size() > 0)
202 setSelectedIndex(0);
206 * Constructor. Creates new empty JComboBox. ComboBox's data model is set to
207 * DefaultComboBoxModel.
209 public JComboBox()
211 this(new DefaultComboBoxModel());
215 * This method returns true JComboBox is editable and false otherwise
217 * @return boolean true if JComboBox is editable and false otherwise
219 public boolean isEditable()
221 return isEditable;
225 * This method adds ancestor listener to this JComboBox.
227 protected void installAncestorListener()
229 /* FIXME: Need to implement.
231 * Need to add ancestor listener to this JComboBox. This listener
232 * should close combo box's popup list of items whenever it
233 * receives an AncestorEvent.
238 * Set the "UI" property of the combo box, which is a look and feel class
239 * responsible for handling comboBox's input events and painting it.
241 * @param ui The new "UI" property
243 public void setUI(ComboBoxUI ui)
245 super.setUI(ui);
249 * This method sets this comboBox's UI to the UIManager's default for the
250 * current look and feel.
252 public void updateUI()
254 setUI((ComboBoxUI) UIManager.getUI(this));
255 invalidate();
259 * This method returns the String identifier for the UI class to the used
260 * with the JComboBox.
262 * @return The String identifier for the UI class.
264 public String getUIClassID()
266 return "ComboBoxUI";
270 * This method returns the UI used to display the JComboBox.
272 * @return The UI used to display the JComboBox.
274 public ComboBoxUI getUI()
276 return (ComboBoxUI) ui;
280 * Set the data model for this JComboBox. This un-registers all listeners
281 * associated with the current model, and re-registers them with the new
282 * model.
284 * @param newDataModel The new data model for this JComboBox
286 public void setModel(ComboBoxModel newDataModel)
288 // dataModel is null if it this method is called from inside the constructors.
289 if (dataModel != null)
291 // Prevents unneccessary updates.
292 if (dataModel == newDataModel)
293 return;
295 // Removes itself (as DataListener) from the to-be-replaced model.
296 dataModel.removeListDataListener(this);
299 /* Adds itself as a DataListener to the new model.
300 * It is intentioned that this operation will fail with a NullPointerException if the
301 * caller delivered a null argument.
303 newDataModel.addListDataListener(this);
305 // Stores old data model for event notification.
306 ComboBoxModel oldDataModel = dataModel;
307 dataModel = newDataModel;
308 selectedItemReminder = newDataModel.getSelectedItem();
310 // Notifies the listeners of the model change.
311 firePropertyChange("model", oldDataModel, dataModel);
315 * This method returns data model for this comboBox.
317 * @return ComboBoxModel containing items for this combo box.
319 public ComboBoxModel getModel()
321 return dataModel;
325 * This method sets JComboBox's popup to be either lightweight or
326 * heavyweight. If 'enabled' is true then lightweight popup is used and
327 * heavyweight otherwise. By default lightweight popup is used to display
328 * this JComboBox's elements.
330 * @param enabled indicates if lightweight popup or heavyweight popup should
331 * be used to display JComboBox's elements.
333 public void setLightWeightPopupEnabled(boolean enabled)
335 lightWeightPopupEnabled = enabled;
339 * This method returns whether popup menu that is used to display list of
340 * combo box's item is lightWeight or not.
342 * @return boolean true if popup menu is lightweight and false otherwise.
344 public boolean isLightWeightPopupEnabled()
346 return lightWeightPopupEnabled;
350 * This method sets editability of the combo box. If combo box is editable
351 * the user can choose component from the combo box list by typing
352 * component's name in the editor(JTextfield by default). Otherwise if not
353 * editable, the user should use the list to choose the component. This
354 * method fires PropertyChangeEvents to JComboBox's registered
355 * PropertyChangeListeners to indicate that 'editable' property of the
356 * JComboBox has changed.
358 * @param editable indicates if the JComboBox's textfield should be editable
359 * or not.
361 public void setEditable(boolean editable)
363 if (isEditable != editable)
365 isEditable = editable;
366 firePropertyChange("editable", !isEditable, isEditable);
371 * Sets number of rows that should be visible in this JComboBox's popup. If
372 * this JComboBox's popup has more elements that maximum number or rows
373 * then popup will have a scroll pane to allow users to view other
374 * elements.
376 * @param rowCount number of rows that will be visible in JComboBox's popup.
378 public void setMaximumRowCount(int rowCount)
380 if (maximumRowCount != rowCount)
382 int oldMaximumRowCount = maximumRowCount;
383 maximumRowCount = rowCount;
384 firePropertyChange("maximumRowCount", oldMaximumRowCount,
385 maximumRowCount);
390 * This method returns number of rows visible in the JComboBox's list of
391 * items.
393 * @return int maximun number of visible rows in the JComboBox's list.
395 public int getMaximumRowCount()
397 return maximumRowCount;
401 * This method sets cell renderer for this JComboBox that will be used to
402 * paint combo box's items. The Renderer should only be used only when
403 * JComboBox is not editable. In the case when JComboBox is editable the
404 * editor must be used. This method also fires PropertyChangeEvent when
405 * cellRendered for this JComboBox has changed.
407 * @param aRenderer cell renderer that will be used by this JComboBox to
408 * paint its elements.
410 public void setRenderer(ListCellRenderer aRenderer)
412 if (renderer != aRenderer)
414 ListCellRenderer oldRenderer = renderer;
415 renderer = aRenderer;
416 firePropertyChange("renderer", oldRenderer, renderer);
421 * This method returns renderer responsible for rendering selected item in
422 * the combo box
424 * @return ListCellRenderer
426 public ListCellRenderer getRenderer()
428 return renderer;
432 * Sets editor for this JComboBox
434 * @param newEditor ComboBoxEditor for this JComboBox. This method fires
435 * PropertyChangeEvent when 'editor' property is changed.
437 public void setEditor(ComboBoxEditor newEditor)
439 if (editor == newEditor)
440 return;
442 if (editor != null)
443 editor.removeActionListener(this);
445 ComboBoxEditor oldEditor = editor;
446 editor = newEditor;
448 if (editor != null)
449 editor.addActionListener(this);
451 firePropertyChange("editor", oldEditor, editor);
455 * Returns editor component that is responsible for displaying/editing
456 * selected item in the combo box.
458 * @return ComboBoxEditor
460 public ComboBoxEditor getEditor()
462 return editor;
466 * Forces combo box to select given item
468 * @param item element in the combo box to select.
470 public void setSelectedItem(Object item)
472 dataModel.setSelectedItem(item);
476 * Returns currently selected item in the combo box.
477 * The result may be <code>null</code> to indicate that nothing is
478 * currently selected.
480 * @return element that is currently selected in this combo box.
482 public Object getSelectedItem()
484 return dataModel.getSelectedItem();
488 * Forces JComboBox to select component located in the given index in the
489 * combo box.
490 * <p>If the index is below -1 or exceeds the upper bound an
491 * <code>IllegalArgumentException</code> is thrown.<p/>
492 * <p>If the index is -1 then no item gets selected.</p>
494 * @param index index specifying location of the component that should be
495 * selected.
497 public void setSelectedIndex(int index)
499 if (index < -1 || index >= dataModel.getSize())
500 // Fails because index is out of bounds.
501 throw new IllegalArgumentException("illegal index: " + index);
502 else
503 // Selects the item at the given index or clears the selection if the
504 // index value is -1.
505 setSelectedItem((index == -1) ? null : dataModel.getElementAt(index));
509 * Returns index of the item that is currently selected in the combo box. If
510 * no item is currently selected, then -1 is returned.
511 * <p>
512 * Note: For performance reasons you should minimize invocation of this
513 * method. If the data model is not an instance of
514 * <code>DefaultComboBoxModel</code> the complexity is O(n) where n is the
515 * number of elements in the combo box.
516 * </p>
518 * @return int Index specifying location of the currently selected item in the
519 * combo box or -1 if nothing is selected in the combo box.
521 public int getSelectedIndex()
523 Object selectedItem = getSelectedItem();
525 if (selectedItem != null)
527 if (dataModel instanceof DefaultComboBoxModel)
528 // Uses special method of DefaultComboBoxModel to retrieve the index.
529 return ((DefaultComboBoxModel) dataModel).getIndexOf(selectedItem);
530 else
532 // Iterates over all items to retrieve the index.
533 int size = dataModel.getSize();
535 for (int i = 0; i < size; i++)
537 Object o = dataModel.getElementAt(i);
539 // XXX: Is special handling of ComparableS neccessary?
540 if ((selectedItem != null) ? selectedItem.equals(o) : o == null)
541 return i;
546 // returns that no item is currently selected
547 return -1;
551 * Returns an object that is used as the display value when calculating the
552 * preferred size for the combo box. This value is, of course, never
553 * displayed anywhere.
555 * @return The prototype display value (possibly <code>null</code>).
557 * @since 1.4
558 * @see #setPrototypeDisplayValue(Object)
560 public Object getPrototypeDisplayValue()
562 return prototypeDisplayValue;
566 * Sets the object that is assumed to be the displayed item when calculating
567 * the preferred size for the combo box. A {@link PropertyChangeEvent} (with
568 * the name <code>prototypeDisplayValue</code>) is sent to all registered
569 * listeners.
571 * @param value the new value (<code>null</code> permitted).
573 * @since 1.4
574 * @see #getPrototypeDisplayValue()
576 public void setPrototypeDisplayValue(Object value)
578 Object oldValue = prototypeDisplayValue;
579 prototypeDisplayValue = value;
580 firePropertyChange("prototypeDisplayValue", oldValue, value);
584 * This method adds given element to this JComboBox.
585 * <p>A <code>RuntimeException</code> is thrown if the data model is not
586 * an instance of {@link MutableComboBoxModel}.</p>
588 * @param element element to add
590 public void addItem(Object element)
592 if (dataModel instanceof MutableComboBoxModel)
593 ((MutableComboBoxModel) dataModel).addElement(element);
594 else
595 throw new RuntimeException("Unable to add the item because the data "
596 + "model it is not an instance of "
597 + "MutableComboBoxModel.");
601 * Inserts given element at the specified index to this JComboBox.
602 * <p>A <code>RuntimeException</code> is thrown if the data model is not
603 * an instance of {@link MutableComboBoxModel}.</p>
605 * @param element element to insert
606 * @param index position where to insert the element
608 public void insertItemAt(Object element, int index)
610 if (dataModel instanceof MutableComboBoxModel)
611 ((MutableComboBoxModel) dataModel).insertElementAt(element, index);
612 else
613 throw new RuntimeException("Unable to insert the item because the data "
614 + "model it is not an instance of "
615 + "MutableComboBoxModel.");
619 * This method removes given element from this JComboBox.
620 * <p>A <code>RuntimeException</code> is thrown if the data model is not
621 * an instance of {@link MutableComboBoxModel}.</p>
623 * @param element element to remove
625 public void removeItem(Object element)
627 if (dataModel instanceof MutableComboBoxModel)
628 ((MutableComboBoxModel) dataModel).removeElement(element);
629 else
630 throw new RuntimeException("Unable to remove the item because the data "
631 + "model it is not an instance of "
632 + "MutableComboBoxModel.");
636 * This method remove element location in the specified index in the
637 * JComboBox.
638 * <p>A <code>RuntimeException</code> is thrown if the data model is not
639 * an instance of {@link MutableComboBoxModel}.</p>
641 * @param index index specifying position of the element to remove
643 public void removeItemAt(int index)
645 if (dataModel instanceof MutableComboBoxModel)
646 ((MutableComboBoxModel) dataModel).removeElementAt(index);
647 else
648 throw new RuntimeException("Unable to remove the item because the data "
649 + "model it is not an instance of "
650 + "MutableComboBoxModel.");
654 * This method removes all elements from this JComboBox.
655 * <p>
656 * A <code>RuntimeException</code> is thrown if the data model is not an
657 * instance of {@link MutableComboBoxModel}.
658 * </p>
660 public void removeAllItems()
662 if (dataModel instanceof DefaultComboBoxModel)
663 // Uses special method if we have a DefaultComboBoxModel.
664 ((DefaultComboBoxModel) dataModel).removeAllElements();
665 else if (dataModel instanceof MutableComboBoxModel)
667 // Iterates over all items and removes each.
668 MutableComboBoxModel mcbm = (MutableComboBoxModel) dataModel;
670 // We intentionally remove the items backwards to support models which
671 // shift their content to the beginning (e.g. linked lists)
672 for (int i = mcbm.getSize() - 1; i >= 0; i--)
673 mcbm.removeElementAt(i);
675 else
676 throw new RuntimeException("Unable to remove the items because the data "
677 +"model it is not an instance of "
678 + "MutableComboBoxModel.");
682 * This method displays popup with list of combo box's items on the screen
684 public void showPopup()
686 setPopupVisible(true);
690 * This method hides popup containing list of combo box's items
692 public void hidePopup()
694 setPopupVisible(false);
698 * This method either displayes or hides the popup containing list of combo
699 * box's items.
701 * @param visible show popup if 'visible' is true and hide it otherwise
703 public void setPopupVisible(boolean visible)
705 getUI().setPopupVisible(this, visible);
709 * Checks if popup is currently visible on the screen.
711 * @return boolean true if popup is visible and false otherwise
713 public boolean isPopupVisible()
715 return getUI().isPopupVisible(this);
719 * This method sets actionCommand to the specified string. ActionEvent fired
720 * to this JComboBox registered ActionListeners will contain this
721 * actionCommand.
723 * @param aCommand new action command for the JComboBox's ActionEvent
725 public void setActionCommand(String aCommand)
727 actionCommand = aCommand;
731 * Returns actionCommand associated with the ActionEvent fired by the
732 * JComboBox to its registered ActionListeners.
734 * @return String actionCommand for the ActionEvent
736 public String getActionCommand()
738 return actionCommand;
742 * setAction
744 * @param a action to set
746 public void setAction(Action a)
748 Action old = action;
749 action = a;
750 configurePropertiesFromAction(action);
751 if (action != null)
752 // FIXME: remove from old action and add to new action
753 // PropertyChangeListener to listen to changes in the action
754 addActionListener(action);
758 * This method returns Action that is invoked when selected item is changed
759 * in the JComboBox.
761 * @return Action
763 public Action getAction()
765 return action;
769 * Configure properties of the JComboBox by reading properties of specified
770 * action. This method always sets the comboBox's "enabled" property to the
771 * value of the Action's "enabled" property.
773 * @param a An Action to configure the combo box from
775 protected void configurePropertiesFromAction(Action a)
777 if (a == null)
779 setEnabled(true);
780 setToolTipText(null);
782 else
784 setEnabled(a.isEnabled());
785 setToolTipText((String) (a.getValue(Action.SHORT_DESCRIPTION)));
790 * Creates PropertyChangeListener to listen for the changes in comboBox's
791 * action properties.
793 * @param action action to listen to for property changes
795 * @return a PropertyChangeListener that listens to changes in
796 * action properties.
798 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
800 return new PropertyChangeListener()
802 public void propertyChange(PropertyChangeEvent e)
804 Action act = (Action) (e.getSource());
805 configurePropertiesFromAction(act);
811 * This method fires ItemEvent to this JComboBox's registered ItemListeners.
812 * This method is invoked when currently selected item in this combo box
813 * has changed.
815 * @param e the ItemEvent describing the change in the combo box's
816 * selection.
818 protected void fireItemStateChanged(ItemEvent e)
820 ItemListener[] ll = getItemListeners();
822 for (int i = 0; i < ll.length; i++)
823 ll[i].itemStateChanged(e);
827 * This method fires ActionEvent to this JComboBox's registered
828 * ActionListeners. This method is invoked when user explicitly changes
829 * currently selected item.
831 protected void fireActionEvent()
833 ActionListener[] ll = getActionListeners();
835 for (int i = 0; i < ll.length; i++)
836 ll[i].actionPerformed(new ActionEvent(this,
837 ActionEvent.ACTION_PERFORMED,
838 actionCommand));
842 * Fires a popupMenuCanceled() event to all <code>PopupMenuListeners</code>.
844 * Note: This method is intended for use by plaf classes only.
846 public void firePopupMenuCanceled()
848 PopupMenuListener[] listeners = getPopupMenuListeners();
849 PopupMenuEvent e = new PopupMenuEvent(this);
850 for(int i = 0; i < listeners.length; i++)
851 listeners[i].popupMenuCanceled(e);
855 * Fires a popupMenuWillBecomeInvisible() event to all
856 * <code>PopupMenuListeners</code>.
858 * Note: This method is intended for use by plaf classes only.
860 public void firePopupMenuWillBecomeInvisible()
862 PopupMenuListener[] listeners = getPopupMenuListeners();
863 PopupMenuEvent e = new PopupMenuEvent(this);
864 for(int i = 0; i < listeners.length; i++)
865 listeners[i].popupMenuWillBecomeInvisible(e);
869 * Fires a popupMenuWillBecomeVisible() event to all
870 * <code>PopupMenuListeners</code>.
872 * Note: This method is intended for use by plaf classes only.
874 public void firePopupMenuWillBecomeVisible()
876 PopupMenuListener[] listeners = getPopupMenuListeners();
877 PopupMenuEvent e = new PopupMenuEvent(this);
878 for(int i = 0; i < listeners.length; i++)
879 listeners[i].popupMenuWillBecomeVisible(e);
883 * This method is invoked whenever selected item changes in the combo box's
884 * data model. It fires ItemEvent and ActionEvent to all registered
885 * ComboBox's ItemListeners and ActionListeners respectively, indicating
886 * the change.
888 protected void selectedItemChanged()
890 // Fire ItemEvent to indicated that previously selected item is now
891 // deselected
892 if (selectedItemReminder != null)
893 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
894 selectedItemReminder,
895 ItemEvent.DESELECTED));
897 // Fire ItemEvent to indicate that new item is selected
898 Object newSelection = getSelectedItem();
899 if (newSelection != null)
900 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
901 newSelection, ItemEvent.SELECTED));
903 // Fire Action Event to JComboBox's registered listeners
904 fireActionEvent();
906 selectedItemReminder = newSelection;
910 * Returns Object array of size 1 containing currently selected element in
911 * the JComboBox.
913 * @return Object[] Object array of size 1 containing currently selected
914 * element in the JComboBox.
916 public Object[] getSelectedObjects()
918 return new Object[] { getSelectedItem() };
922 * This method handles actionEvents fired by the ComboBoxEditor. It changes
923 * this JComboBox's selection to the new value currently in the editor and
924 * hides list of combo box items.
926 * @param e the ActionEvent
928 public void actionPerformed(ActionEvent e)
930 setSelectedItem(((ComboBoxEditor) e.getSource()).getItem());
931 setPopupVisible(false);
935 * This method selects item in this combo box that matches specified
936 * specified keyChar and returns true if such item is found. Otherwise
937 * false is returned.
939 * @param keyChar character indicating which item in the combo box should be
940 * selected.
942 * @return boolean true if item corresponding to the specified keyChar
943 * exists in the combo box. Otherwise false is returned.
945 public boolean selectWithKeyChar(char keyChar)
947 // FIXME: Need to implement
948 return false;
952 * The part of implementation of ListDataListener interface. This method is
953 * invoked when some items where added to the JComboBox's data model.
955 * @param event ListDataEvent describing the change
957 public void intervalAdded(ListDataEvent event)
959 // FIXME: Need to implement
960 repaint();
964 * The part of implementation of ListDataListener interface. This method is
965 * invoked when some items where removed from the JComboBox's data model.
967 * @param event ListDataEvent describing the change.
969 public void intervalRemoved(ListDataEvent event)
971 // FIXME: Need to implement
972 repaint();
976 * The part of implementation of ListDataListener interface. This method is
977 * invoked when contents of the JComboBox's data model changed.
979 * @param event ListDataEvent describing the change
981 public void contentsChanged(ListDataEvent event)
983 // if first and last index of the given ListDataEvent are both -1,
984 // then it indicates that selected item in the combo box data model
985 // have changed.
986 if (event.getIndex0() == -1 && event.getIndex1() == -1)
987 selectedItemChanged();
991 * This method disables or enables JComboBox. If the JComboBox is enabled,
992 * then user is able to make item choice, otherwise if JComboBox is
993 * disabled then user is not able to make a selection.
995 * @param enabled if 'enabled' is true then enable JComboBox and disable it
997 public void setEnabled(boolean enabled)
999 boolean oldEnabled = super.isEnabled();
1000 if (enabled != oldEnabled)
1002 super.setEnabled(enabled);
1003 firePropertyChange("enabled", oldEnabled, enabled);
1008 * This method initializes specified ComboBoxEditor to display given item.
1010 * @param anEditor ComboBoxEditor to initialize
1011 * @param anItem Item that should displayed in the specified editor
1013 public void configureEditor(ComboBoxEditor anEditor, Object anItem)
1015 anEditor.setItem(anItem);
1019 * This method hides combo box's popup whenever TAB key is pressed.
1021 * @param e The KeyEvent indicating which key was pressed.
1023 public void processKeyEvent(KeyEvent e)
1025 if (e.getKeyCode() == KeyEvent.VK_TAB)
1026 setPopupVisible(false);
1027 else if (keySelectionManager != null)
1029 int i = keySelectionManager.selectionForKey(e.getKeyChar(),
1030 getModel());
1031 if (i >= 0)
1032 setSelectedIndex(i);
1033 else
1034 super.processKeyEvent(e);
1036 else
1037 super.processKeyEvent(e);
1041 * setKeySelectionManager
1043 * @param aManager
1045 public void setKeySelectionManager(KeySelectionManager aManager)
1047 keySelectionManager = aManager;
1051 * getKeySelectionManager
1053 * @return JComboBox.KeySelectionManager
1055 public KeySelectionManager getKeySelectionManager()
1057 return null;
1061 * This method returns number of elements in this JComboBox
1063 * @return int number of elements in this JComboBox
1065 public int getItemCount()
1067 return dataModel.getSize();
1071 * Returns elements located in the combo box at the given index.
1073 * @param index index specifying location of the component to return.
1075 * @return component in the combo box that is located in the given index.
1077 public Object getItemAt(int index)
1079 return dataModel.getElementAt(index);
1083 * createDefaultKeySelectionManager
1085 * @return KeySelectionManager
1087 protected KeySelectionManager createDefaultKeySelectionManager()
1089 return null;
1093 * A string that describes this JComboBox. Normally only used for debugging.
1095 * @return A string describing this JComboBox
1097 protected String paramString()
1099 return "JComboBox";
1102 public AccessibleContext getAccessibleContext()
1104 if (accessibleContext == null)
1105 accessibleContext = new AccessibleJComboBox();
1107 return accessibleContext;
1111 * This methods adds specified ActionListener to this JComboBox.
1113 * @param listener to add
1115 public void addActionListener(ActionListener listener)
1117 listenerList.add(ActionListener.class, listener);
1121 * This method removes specified ActionListener from this JComboBox.
1123 * @param listener ActionListener
1125 public void removeActionListener(ActionListener listener)
1127 listenerList.remove(ActionListener.class, listener);
1131 * This method returns array of ActionListeners that are registered with
1132 * this JComboBox.
1134 * @since 1.4
1136 public ActionListener[] getActionListeners()
1138 return (ActionListener[]) getListeners(ActionListener.class);
1142 * This method registers given ItemListener with this JComboBox
1144 * @param listener to remove
1146 public void addItemListener(ItemListener listener)
1148 listenerList.add(ItemListener.class, listener);
1152 * This method unregisters given ItemListener from this JComboBox
1154 * @param listener to remove
1156 public void removeItemListener(ItemListener listener)
1158 listenerList.remove(ItemListener.class, listener);
1162 * This method returns array of ItemListeners that are registered with this
1163 * JComboBox.
1165 * @since 1.4
1167 public ItemListener[] getItemListeners()
1169 return (ItemListener[]) getListeners(ItemListener.class);
1173 * Adds PopupMenuListener to combo box to listen to the events fired by the
1174 * combo box's popup menu containing its list of items
1176 * @param listener to add
1178 public void addPopupMenuListener(PopupMenuListener listener)
1180 listenerList.add(PopupMenuListener.class, listener);
1184 * Removes PopupMenuListener to combo box to listen to the events fired by
1185 * the combo box's popup menu containing its list of items
1187 * @param listener to add
1189 public void removePopupMenuListener(PopupMenuListener listener)
1191 listenerList.remove(PopupMenuListener.class, listener);
1195 * Returns array of PopupMenuListeners that are registered with combo box.
1197 public PopupMenuListener[] getPopupMenuListeners()
1199 return (PopupMenuListener[]) getListeners(PopupMenuListener.class);
1203 * Accessibility support for <code>JComboBox</code>.
1205 protected class AccessibleJComboBox extends AccessibleJComponent
1206 implements AccessibleAction, AccessibleSelection
1208 private static final long serialVersionUID = 8217828307256675666L;
1210 protected AccessibleJComboBox()
1212 // Nothing to do here.
1215 public int getAccessibleChildrenCount()
1217 return 0;
1220 public Accessible getAccessibleChild(int value0)
1222 return null;
1225 public AccessibleSelection getAccessibleSelection()
1227 return null;
1230 public Accessible getAccessibleSelection(int value0)
1232 return null;
1235 public boolean isAccessibleChildSelected(int value0)
1237 return false;
1240 public AccessibleRole getAccessibleRole()
1242 return AccessibleRole.COMBO_BOX;
1245 public AccessibleAction getAccessibleAction()
1247 return null;
1250 public String getAccessibleActionDescription(int value0)
1252 return null;
1255 public int getAccessibleActionCount()
1257 return 0;
1260 public boolean doAccessibleAction(int value0)
1262 return false;
1265 public int getAccessibleSelectionCount()
1267 return 0;
1270 public void addAccessibleSelection(int value0)
1272 // TODO: Implement this properly.
1275 public void removeAccessibleSelection(int value0)
1277 // TODO: Implement this properly.
1280 public void clearAccessibleSelection()
1282 // TODO: Implement this properly.
1285 public void selectAllAccessibleSelection()
1287 // TODO: Implement this properly.