KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JComboBox


1 /*
2  * @(#)JComboBox.java 1.126 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.beans.*;
10 import java.util.*;
11
12 import java.awt.*;
13 import java.awt.event.*;
14
15 import java.io.Serializable JavaDoc;
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20 import javax.swing.event.*;
21 import javax.swing.plaf.*;
22 import javax.swing.border.*;
23
24 import javax.accessibility.*;
25
26 /**
27  * A component that combines a button or editable field and a drop-down list.
28  * The user can select a value from the drop-down list, which appears at the
29  * user's request. If you make the combo box editable, then the combo box
30  * includes an editable field into which the user can type a value.
31  *
32  * <p>
33  * For the keyboard keys used by this component in the standard Look and
34  * Feel (L&F) renditions, see the
35  * <a HREF="doc-files/Key-Index.html#JComboBox"><code>JComboBox</code> key assignments</a>.
36  * <p>
37  * <strong>Warning:</strong>
38  * Serialized objects of this class will not be compatible with
39  * future Swing releases. The current serialization support is
40  * appropriate for short term storage or RMI between applications running
41  * the same version of Swing. As of 1.4, support for long term storage
42  * of all JavaBeans<sup><font size="-2">TM</font></sup>
43  * has been added to the <code>java.beans</code> package.
44  * Please see {@link java.beans.XMLEncoder}.
45  *
46  * <p>
47  * See <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html">How to Use Combo Boxes</a>
48  * in <a HREF="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
49  * for further information.
50  * <p>
51  * @see ComboBoxModel
52  * @see DefaultComboBoxModel
53  *
54  * @beaninfo
55  * attribute: isContainer false
56  * description: A combination of a text field and a drop-down list.
57  *
58  * @version 1.126 05/05/04
59  * @author Arnaud Weber
60  * @author Mark Davidson
61  */

62 public class JComboBox extends JComponent JavaDoc
63 implements ItemSelectable,ListDataListener,ActionListener, Accessible {
64     /**
65      * @see #getUIClassID
66      * @see #readObject
67      */

68     private static final String JavaDoc uiClassID = "ComboBoxUI";
69
70     /**
71      * This protected field is implementation specific. Do not access directly
72      * or override. Use the accessor methods instead.
73      *
74      * @see #getModel
75      * @see #setModel
76      */

77     protected ComboBoxModel JavaDoc dataModel;
78     /**
79      * This protected field is implementation specific. Do not access directly
80      * or override. Use the accessor methods instead.
81      *
82      * @see #getRenderer
83      * @see #setRenderer
84      */

85     protected ListCellRenderer JavaDoc renderer;
86     /**
87      * This protected field is implementation specific. Do not access directly
88      * or override. Use the accessor methods instead.
89      *
90      * @see #getEditor
91      * @see #setEditor
92      */

93     protected ComboBoxEditor JavaDoc editor;
94     /**
95      * This protected field is implementation specific. Do not access directly
96      * or override. Use the accessor methods instead.
97      *
98      * @see #getMaximumRowCount
99      * @see #setMaximumRowCount
100      */

101     protected int maximumRowCount = 8;
102
103     /**
104      * This protected field is implementation specific. Do not access directly
105      * or override. Use the accessor methods instead.
106      *
107      * @see #isEditable
108      * @see #setEditable
109      */

110     protected boolean isEditable = false;
111     /**
112      * This protected field is implementation specific. Do not access directly
113      * or override. Use the accessor methods instead.
114      *
115      * @see #setKeySelectionManager
116      * @see #getKeySelectionManager
117      */

118     protected KeySelectionManager keySelectionManager = null;
119     /**
120      * This protected field is implementation specific. Do not access directly
121      * or override. Use the accessor methods instead.
122      *
123      * @see #setActionCommand
124      * @see #getActionCommand
125      */

126     protected String JavaDoc actionCommand = "comboBoxChanged";
127     /**
128      * This protected field is implementation specific. Do not access directly
129      * or override. Use the accessor methods instead.
130      *
131      * @see #setLightWeightPopupEnabled
132      * @see #isLightWeightPopupEnabled
133      */

134     protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled();
135
136     /**
137      * This protected field is implementation specific. Do not access directly
138      * or override.
139      */

140     protected Object JavaDoc selectedItemReminder = null;
141
142     private Object JavaDoc prototypeDisplayValue;
143
144     // Flag to ensure that infinite loops do not occur with ActionEvents.
145
private boolean firingActionEvent = false;
146
147     // Flag to ensure the we don't get multiple ActionEvents on item selection.
148
private boolean selectingItem = false;
149
150     /**
151      * Creates a <code>JComboBox</code> that takes it's items from an
152      * existing <code>ComboBoxModel</code>. Since the
153      * <code>ComboBoxModel</code> is provided, a combo box created using
154      * this constructor does not create a default combo box model and
155      * may impact how the insert, remove and add methods behave.
156      *
157      * @param aModel the <code>ComboBoxModel</code> that provides the
158      * displayed list of items
159      * @see DefaultComboBoxModel
160      */

161     public JComboBox(ComboBoxModel JavaDoc aModel) {
162         super();
163         setModel(aModel);
164         init();
165     }
166
167     /**
168      * Creates a <code>JComboBox</code> that contains the elements
169      * in the specified array. By default the first item in the array
170      * (and therefore the data model) becomes selected.
171      *
172      * @param items an array of objects to insert into the combo box
173      * @see DefaultComboBoxModel
174      */

175     public JComboBox(final Object JavaDoc items[]) {
176         super();
177         setModel(new DefaultComboBoxModel JavaDoc(items));
178         init();
179     }
180
181     /**
182      * Creates a <code>JComboBox</code> that contains the elements
183      * in the specified Vector. By default the first item in the vector
184      * and therefore the data model) becomes selected.
185      *
186      * @param items an array of vectors to insert into the combo box
187      * @see DefaultComboBoxModel
188      */

189     public JComboBox(Vector<?> items) {
190         super();
191         setModel(new DefaultComboBoxModel JavaDoc(items));
192         init();
193     }
194
195     /**
196      * Creates a <code>JComboBox</code> with a default data model.
197      * The default data model is an empty list of objects.
198      * Use <code>addItem</code> to add items. By default the first item
199      * in the data model becomes selected.
200      *
201      * @see DefaultComboBoxModel
202      */

203     public JComboBox() {
204         super();
205         setModel(new DefaultComboBoxModel JavaDoc());
206         init();
207     }
208
209     private void init() {
210         installAncestorListener();
211         setOpaque(true);
212         updateUI();
213     }
214
215     protected void installAncestorListener() {
216         addAncestorListener(new AncestorListener(){
217                                 public void ancestorAdded(AncestorEvent event){ hidePopup();}
218                                 public void ancestorRemoved(AncestorEvent event){ hidePopup();}
219                                 public void ancestorMoved(AncestorEvent event){
220                                     if (event.getSource() != JComboBox.this)
221                                         hidePopup();
222                                 }});
223     }
224
225     /**
226      * Sets the L&F object that renders this component.
227      *
228      * @param ui the <code>ComboBoxUI</code> L&F object
229      * @see UIDefaults#getUI
230      *
231      * @beaninfo
232      * bound: true
233      * hidden: true
234      * attribute: visualUpdate true
235      * description: The UI object that implements the Component's LookAndFeel.
236      */

237     public void setUI(ComboBoxUI ui) {
238         super.setUI(ui);
239     }
240
241     /**
242      * Resets the UI property to a value from the current look and feel.
243      *
244      * @see JComponent#updateUI
245      */

246     public void updateUI() {
247         setUI((ComboBoxUI)UIManager.getUI(this));
248     }
249
250
251     /**
252      * Returns the name of the L&F class that renders this component.
253      *
254      * @return the string "ComboBoxUI"
255      * @see JComponent#getUIClassID
256      * @see UIDefaults#getUI
257      */

258     public String JavaDoc getUIClassID() {
259         return uiClassID;
260     }
261
262
263     /**
264      * Returns the L&F object that renders this component.
265      *
266      * @return the ComboBoxUI object that renders this component
267      */

268     public ComboBoxUI getUI() {
269         return(ComboBoxUI)ui;
270     }
271
272     /**
273      * Sets the data model that the <code>JComboBox</code> uses to obtain
274      * the list of items.
275      *
276      * @param aModel the <code>ComboBoxModel</code> that provides the
277      * displayed list of items
278      *
279      * @beaninfo
280      * bound: true
281      * description: Model that the combo box uses to get data to display.
282      */

283     public void setModel(ComboBoxModel JavaDoc aModel) {
284         ComboBoxModel JavaDoc oldModel = dataModel;
285     if (oldModel != null) {
286         oldModel.removeListDataListener(this);
287     }
288         dataModel = aModel;
289     dataModel.addListDataListener(this);
290     
291     // set the current selected item.
292
selectedItemReminder = dataModel.getSelectedItem();
293
294         firePropertyChange( "model", oldModel, dataModel);
295     }
296
297     /**
298      * Returns the data model currently used by the <code>JComboBox</code>.
299      *
300      * @return the <code>ComboBoxModel</code> that provides the displayed
301      * list of items
302      */

303     public ComboBoxModel JavaDoc getModel() {
304         return dataModel;
305     }
306
307     /*
308      * Properties
309      */

310
311     /**
312      * Sets the <code>lightWeightPopupEnabled</code> property, which
313      * provides a hint as to whether or not a lightweight
314      * <code>Component</code> should be used to contain the
315      * <code>JComboBox</code>, versus a heavyweight
316      * <code>Component</code> such as a <code>Panel</code>
317      * or a <code>Window</code>. The decision of lightweight
318      * versus heavyweight is ultimately up to the
319      * <code>JComboBox</code>. Lightweight windows are more
320      * efficient than heavyweight windows, but lightweight
321      * and heavyweight components do not mix well in a GUI.
322      * If your application mixes lightweight and heavyweight
323      * components, you should disable lightweight popups.
324      * The default value for the <code>lightWeightPopupEnabled</code>
325      * property is <code>true</code>, unless otherwise specified
326      * by the look and feel. Some look and feels always use
327      * heavyweight popups, no matter what the value of this property.
328      * <p>
329      * See the article <a HREF="http://java.sun.com/products/jfc/tsc/articles/mixing/index.html">Mixing Heavy and Light Components</a>
330      * on <a HREF="http://java.sun.com/products/jfc/tsc">
331      * <em>The Swing Connection</em></a>
332      * This method fires a property changed event.
333      *
334      * @param aFlag if <code>true</code>, lightweight popups are desired
335      *
336      * @beaninfo
337      * bound: true
338      * expert: true
339      * description: Set to <code>false</code> to require heavyweight popups.
340      */

341     public void setLightWeightPopupEnabled(boolean aFlag) {
342     boolean oldFlag = lightWeightPopupEnabled;
343         lightWeightPopupEnabled = aFlag;
344     firePropertyChange("lightWeightPopupEnabled", oldFlag, lightWeightPopupEnabled);
345     }
346
347     /**
348      * Gets the value of the <code>lightWeightPopupEnabled</code>
349      * property.
350      *
351      * @return the value of the <code>lightWeightPopupEnabled</code>
352      * property
353      * @see #setLightWeightPopupEnabled
354      */

355     public boolean isLightWeightPopupEnabled() {
356         return lightWeightPopupEnabled;
357     }
358
359     /**
360      * Determines whether the <code>JComboBox</code> field is editable.
361      * An editable <code>JComboBox</code> allows the user to type into the
362      * field or selected an item from the list to initialize the field,
363      * after which it can be edited. (The editing affects only the field,
364      * the list item remains intact.) A non editable <code>JComboBox</code>
365      * displays the selected item in the field,
366      * but the selection cannot be modified.
367      *
368      * @param aFlag a boolean value, where true indicates that the
369      * field is editable
370      *
371      * @beaninfo
372      * bound: true
373      * preferred: true
374      * description: If true, the user can type a new value in the combo box.
375      */

376     public void setEditable(boolean aFlag) {
377         boolean oldFlag = isEditable;
378         isEditable = aFlag;
379         firePropertyChange( "editable", oldFlag, isEditable );
380     }
381
382     /**
383      * Returns true if the <code>JComboBox</code> is editable.
384      * By default, a combo box is not editable.
385      *
386      * @return true if the <code>JComboBox</code> is editable, else false
387      */

388     public boolean isEditable() {
389         return isEditable;
390     }
391
392     /**
393      * Sets the maximum number of rows the <code>JComboBox</code> displays.
394      * If the number of objects in the model is greater than count,
395      * the combo box uses a scrollbar.
396      *
397      * @param count an integer specifying the maximum number of items to
398      * display in the list before using a scrollbar
399      * @beaninfo
400      * bound: true
401      * preferred: true
402      * description: The maximum number of rows the popup should have
403      */

404     public void setMaximumRowCount(int count) {
405         int oldCount = maximumRowCount;
406         maximumRowCount = count;
407         firePropertyChange( "maximumRowCount", oldCount, maximumRowCount );
408     }
409
410     /**
411      * Returns the maximum number of items the combo box can display
412      * without a scrollbar
413      *
414      * @return an integer specifying the maximum number of items that are
415      * displayed in the list before using a scrollbar
416      */

417     public int getMaximumRowCount() {
418         return maximumRowCount;
419     }
420
421     /**
422      * Sets the renderer that paints the list items and the item selected from the list in
423      * the JComboBox field. The renderer is used if the JComboBox is not
424      * editable. If it is editable, the editor is used to render and edit
425      * the selected item.
426      * <p>
427      * The default renderer displays a string or an icon.
428      * Other renderers can handle graphic images and composite items.
429      * <p>
430      * To display the selected item,
431      * <code>aRenderer.getListCellRendererComponent</code>
432      * is called, passing the list object and an index of -1.
433      *
434      * @param aRenderer the <code>ListCellRenderer</code> that
435      * displays the selected item
436      * @see #setEditor
437      * @beaninfo
438      * bound: true
439      * expert: true
440      * description: The renderer that paints the item selected in the list.
441      */

442     public void setRenderer(ListCellRenderer JavaDoc aRenderer) {
443         ListCellRenderer JavaDoc oldRenderer = renderer;
444         renderer = aRenderer;
445         firePropertyChange( "renderer", oldRenderer, renderer );
446         invalidate();
447     }
448
449     /**
450      * Returns the renderer used to display the selected item in the
451      * <code>JComboBox</code> field.
452      *
453      * @return the <code>ListCellRenderer</code> that displays
454      * the selected item.
455      */

456     public ListCellRenderer JavaDoc getRenderer() {
457         return renderer;
458     }
459
460     /**
461      * Sets the editor used to paint and edit the selected item in the
462      * <code>JComboBox</code> field. The editor is used only if the
463      * receiving <code>JComboBox</code> is editable. If not editable,
464      * the combo box uses the renderer to paint the selected item.
465      *
466      * @param anEditor the <code>ComboBoxEditor</code> that
467      * displays the selected item
468      * @see #setRenderer
469      * @beaninfo
470      * bound: true
471      * expert: true
472      * description: The editor that combo box uses to edit the current value
473      */

474     public void setEditor(ComboBoxEditor JavaDoc anEditor) {
475         ComboBoxEditor JavaDoc oldEditor = editor;
476
477         if ( editor != null ) {
478             editor.removeActionListener(this);
479     }
480         editor = anEditor;
481         if ( editor != null ) {
482             editor.addActionListener(this);
483         }
484         firePropertyChange( "editor", oldEditor, editor );
485     }
486
487     /**
488      * Returns the editor used to paint and edit the selected item in the
489      * <code>JComboBox</code> field.
490      *
491      * @return the <code>ComboBoxEditor</code> that displays the selected item
492      */

493     public ComboBoxEditor JavaDoc getEditor() {
494         return editor;
495     }
496
497     //
498
// Selection
499
//
500

501     /**
502      * Sets the selected item in the combo box display area to the object in
503      * the argument.
504      * If <code>anObject</code> is in the list, the display area shows
505      * <code>anObject</code> selected.
506      * <p>
507      * If <code>anObject</code> is <i>not</i> in the list and the combo box is
508      * uneditable, it will not change the current selection. For editable
509      * combo boxes, the selection will change to <code>anObject</code>.
510      * <p>
511      * If this constitutes a change in the selected item,
512      * <code>ItemListener</code>s added to the combo box will be notified with
513      * one or two <code>ItemEvent</code>s.
514      * If there is a current selected item, an <code>ItemEvent</code> will be
515      * fired and the state change will be <code>ItemEvent.DESELECTED</code>.
516      * If <code>anObject</code> is in the list and is not currently selected
517      * then an <code>ItemEvent</code> will be fired and the state change will
518      * be <code>ItemEvent.SELECTED</code>.
519      * <p>
520      * <code>ActionListener</code>s added to the combo box will be notified
521      * with an <code>ActionEvent</code> when this method is called.
522      *
523      * @param anObject the list object to select; use <code>null</code> to
524                         clear the selection
525      * @beaninfo
526      * preferred: true
527      * description: Sets the selected item in the JComboBox.
528      */

529     public void setSelectedItem(Object JavaDoc anObject) {
530     Object JavaDoc oldSelection = selectedItemReminder;
531     if (oldSelection == null || !oldSelection.equals(anObject)) {
532
533         if (anObject != null && !isEditable()) {
534         // For non editable combo boxes, an invalid selection
535
// will be rejected.
536
boolean found = false;
537         for (int i = 0; i < dataModel.getSize(); i++) {
538             if (anObject.equals(dataModel.getElementAt(i))) {
539             found = true;
540             break;
541             }
542         }
543         if (!found) {
544             return;
545         }
546         }
547         
548         // Must toggle the state of this flag since this method
549
// call may result in ListDataEvents being fired.
550
selectingItem = true;
551         dataModel.setSelectedItem(anObject);
552         selectingItem = false;
553
554         if (selectedItemReminder != dataModel.getSelectedItem()) {
555         // in case a users implementation of ComboBoxModel
556
// doesn't fire a ListDataEvent when the selection
557
// changes.
558
selectedItemChanged();
559         }
560     }
561     fireActionEvent();
562     }
563
564     /**
565      * Returns the current selected item.
566      * <p>
567      * If the combo box is editable, then this value may not have been added
568      * to the combo box with <code>addItem</code>, <code>insertItemAt</code>
569      * or the data constructors.
570      *
571      * @return the current selected Object
572      * @see #setSelectedItem
573      */

574     public Object JavaDoc getSelectedItem() {
575         return dataModel.getSelectedItem();
576     }
577
578     /**
579      * Selects the item at index <code>anIndex</code>.
580      *
581      * @param anIndex an integer specifying the list item to select,
582      * where 0 specifies the first item in the list and -1 indicates no selection
583      * @exception IllegalArgumentException if <code>anIndex</code> < -1 or
584      * <code>anIndex</code> is greater than or equal to size
585      * @beaninfo
586      * preferred: true
587      * description: The item at index is selected.
588      */

589     public void setSelectedIndex(int anIndex) {
590         int size = dataModel.getSize();
591
592         if ( anIndex == -1 ) {
593             setSelectedItem( null );
594         } else if ( anIndex < -1 || anIndex >= size ) {
595             throw new IllegalArgumentException JavaDoc("setSelectedIndex: " + anIndex + " out of bounds");
596         } else {
597             setSelectedItem(dataModel.getElementAt(anIndex));
598         }
599     }
600
601     /**
602      * Returns the first item in the list that matches the given item.
603      * The result is not always defined if the <code>JComboBox</code>
604      * allows selected items that are not in the list.
605      * Returns -1 if there is no selected item or if the user specified
606      * an item which is not in the list.
607      
608      * @return an integer specifying the currently selected list item,
609      * where 0 specifies
610      * the first item in the list;
611      * or -1 if no item is selected or if
612      * the currently selected item is not in the list
613      */

614     public int getSelectedIndex() {
615         Object JavaDoc sObject = dataModel.getSelectedItem();
616         int i,c;
617         Object JavaDoc obj;
618
619         for ( i=0,c=dataModel.getSize();i<c;i++ ) {
620             obj = dataModel.getElementAt(i);
621             if ( obj != null && obj.equals(sObject) )
622                 return i;
623         }
624         return -1;
625     }
626
627     /**
628      * Returns the "prototypical display" value - an Object used
629      * for the calculation of the display height and width.
630      *
631      * @return the value of the <code>prototypeDisplayValue</code> property
632      * @see #setPrototypeDisplayValue
633      * @since 1.4
634      */

635     public Object JavaDoc getPrototypeDisplayValue() {
636         return prototypeDisplayValue;
637     }
638
639     /**
640      * Sets the prototype display value used to calculate the size of the display
641      * for the UI portion.
642      * <p>
643      * If a prototype display value is specified, the preferred size of
644      * the combo box is calculated by configuring the renderer with the
645      * prototype display value and obtaining its preferred size. Specifying
646      * the preferred display value is often useful when the combo box will be
647      * displaying large amounts of data. If no prototype display value has
648      * been specified, the renderer must be configured for each value from
649      * the model and its preferred size obtained, which can be
650      * relatively expensive.
651      *
652      * @param prototypeDisplayValue
653      * @see #getPrototypeDisplayValue
654      * @since 1.4
655      * @beaninfo
656      * bound: true
657      * attribute: visualUpdate true
658      * description: The display prototype value, used to compute display width and height.
659      */

660     public void setPrototypeDisplayValue(Object JavaDoc prototypeDisplayValue) {
661         Object JavaDoc oldValue = this.prototypeDisplayValue;
662         this.prototypeDisplayValue = prototypeDisplayValue;
663         firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue);
664     }
665
666     /**
667      * Adds an item to the item list.
668      * This method works only if the <code>JComboBox</code> uses a
669      * mutable data model.
670      * <p>
671      * <strong>Warning:</strong>
672      * Focus and keyboard navigation problems may arise if you add duplicate
673      * String objects. A workaround is to add new objects instead of String
674      * objects and make sure that the toString() method is defined.
675      * For example:
676      * <pre>
677      * comboBox.addItem(makeObj("Item 1"));
678      * comboBox.addItem(makeObj("Item 1"));
679      * ...
680      * private Object makeObj(final String item) {
681      * return new Object() { public String toString() { return item; } };
682      * }
683      * </pre>
684      *
685      * @param anObject the Object to add to the list
686      * @see MutableComboBoxModel
687      */

688     public void addItem(Object JavaDoc anObject) {
689         checkMutableComboBoxModel();
690         ((MutableComboBoxModel JavaDoc)dataModel).addElement(anObject);
691     }
692
693     /**
694      * Inserts an item into the item list at a given index.
695      * This method works only if the <code>JComboBox</code> uses a
696      * mutable data model.
697      *
698      * @param anObject the <code>Object</code> to add to the list
699      * @param index an integer specifying the position at which
700      * to add the item
701      * @see MutableComboBoxModel
702      */

703     public void insertItemAt(Object JavaDoc anObject, int index) {
704         checkMutableComboBoxModel();
705         ((MutableComboBoxModel JavaDoc)dataModel).insertElementAt(anObject,index);
706     }
707
708     /**
709      * Removes an item from the item list.
710      * This method works only if the <code>JComboBox</code> uses a
711      * mutable data model.
712      *
713      * @param anObject the object to remove from the item list
714      * @see MutableComboBoxModel
715      */

716     public void removeItem(Object JavaDoc anObject) {
717         checkMutableComboBoxModel();
718         ((MutableComboBoxModel JavaDoc)dataModel).removeElement(anObject);
719     }
720
721     /**
722      * Removes the item at <code>anIndex</code>
723      * This method works only if the <code>JComboBox</code> uses a
724      * mutable data model.
725      *
726      * @param anIndex an int specifying the index of the item to remove,
727      * where 0
728      * indicates the first item in the list
729      * @see MutableComboBoxModel
730      */

731     public void removeItemAt(int anIndex) {
732         checkMutableComboBoxModel();
733         ((MutableComboBoxModel JavaDoc)dataModel).removeElementAt( anIndex );
734     }
735
736     /**
737      * Removes all items from the item list.
738      */

739     public void removeAllItems() {
740         checkMutableComboBoxModel();
741         MutableComboBoxModel JavaDoc model = (MutableComboBoxModel JavaDoc)dataModel;
742         int size = model.getSize();
743
744         if ( model instanceof DefaultComboBoxModel JavaDoc ) {
745             ((DefaultComboBoxModel JavaDoc)model).removeAllElements();
746         }
747         else {
748             for ( int i = 0; i < size; ++i ) {
749                 Object JavaDoc element = model.getElementAt( 0 );
750                 model.removeElement( element );
751             }
752         }
753     selectedItemReminder = null;
754     if (isEditable()) {
755         editor.setItem(null);
756     }
757     }
758
759     /**
760      * Checks that the <code>dataModel</code> is an instance of
761      * <code>MutableComboBoxModel</code>. If not, it throws an exception.
762      * @exception RuntimeException if <code>dataModel</code> is not an
763      * instance of <code>MutableComboBoxModel</code>.
764      */

765     void checkMutableComboBoxModel() {
766         if ( !(dataModel instanceof MutableComboBoxModel JavaDoc) )
767             throw new RuntimeException JavaDoc("Cannot use this method with a non-Mutable data model.");
768     }
769
770     /**
771      * Causes the combo box to display its popup window.
772      * @see #setPopupVisible
773      */

774     public void showPopup() {
775         setPopupVisible(true);
776     }
777
778     /**
779      * Causes the combo box to close its popup window.
780      * @see #setPopupVisible
781      */

782     public void hidePopup() {
783         setPopupVisible(false);
784     }
785
786     /**
787      * Sets the visibility of the popup.
788      */

789     public void setPopupVisible(boolean v) {
790         getUI().setPopupVisible(this, v);
791     }
792
793     /**
794      * Determines the visibility of the popup.
795      *
796      * @return true if the popup is visible, otherwise returns false
797      */

798     public boolean isPopupVisible() {
799         return getUI().isPopupVisible(this);
800     }
801
802     /** Selection **/
803
804     /**
805      * Adds an <code>ItemListener</code>.
806      * <p>
807      * <code>aListener</code> will receive one or two <code>ItemEvent</code>s when
808      * the selected item changes.
809      *
810      * @param aListener the <code>ItemListener</code> that is to be notified
811      * @see #setSelectedItem
812      */

813     public void addItemListener(ItemListener aListener) {
814         listenerList.add(ItemListener.class,aListener);
815     }
816
817     /** Removes an <code>ItemListener</code>.
818      *
819      * @param aListener the <code>ItemListener</code> to remove
820      */

821     public void removeItemListener(ItemListener aListener) {
822         listenerList.remove(ItemListener.class,aListener);
823     }
824
825     /**
826      * Returns an array of all the <code>ItemListener</code>s added
827      * to this JComboBox with addItemListener().
828      *
829      * @return all of the <code>ItemListener</code>s added or an empty
830      * array if no listeners have been added
831      * @since 1.4
832      */

833     public ItemListener[] getItemListeners() {
834         return (ItemListener[])listenerList.getListeners(ItemListener.class);
835     }
836
837     /**
838      * Adds an <code>ActionListener</code>.
839      * <p>
840      * The <code>ActionListener</code> will receive an <code>ActionEvent</code>
841      * when a selection has been made. If the combo box is editable, then
842      * an <code>ActionEvent</code> will be fired when editing has stopped.
843      *
844      * @param l the <code>ActionListener</code> that is to be notified
845      * @see #setSelectedItem
846      */

847     public void addActionListener(ActionListener l) {
848         listenerList.add(ActionListener.class,l);
849     }
850
851     /** Removes an <code>ActionListener</code>.
852      *
853      * @param l the <code>ActionListener</code> to remove
854      */

855     public void removeActionListener(ActionListener l) {
856     if ((l != null) && (getAction() == l)) {
857         setAction(null);
858     } else {
859         listenerList.remove(ActionListener.class, l);
860     }
861     }
862
863     /**
864      * Returns an array of all the <code>ActionListener</code>s added
865      * to this JComboBox with addActionListener().
866      *
867      * @return all of the <code>ActionListener</code>s added or an empty
868      * array if no listeners have been added
869      * @since 1.4
870      */

871     public ActionListener[] getActionListeners() {
872         return (ActionListener[])listenerList.getListeners(
873                 ActionListener.class);
874     }
875
876     /**
877      * Adds a <code>PopupMenu</code> listener which will listen to notification
878      * messages from the popup portion of the combo box.
879      * <p>
880      * For all standard look and feels shipped with Java 2, the popup list
881      * portion of combo box is implemented as a <code>JPopupMenu</code>.
882      * A custom look and feel may not implement it this way and will
883      * therefore not receive the notification.
884      *
885      * @param l the <code>PopupMenuListener</code> to add
886      * @since 1.4
887      */

888     public void addPopupMenuListener(PopupMenuListener l) {
889         listenerList.add(PopupMenuListener.class,l);
890     }
891
892     /**
893      * Removes a <code>PopupMenuListener</code>.
894      *
895      * @param l the <code>PopupMenuListener</code> to remove
896      * @see #addPopupMenuListener
897      * @since 1.4
898      */

899     public void removePopupMenuListener(PopupMenuListener l) {
900         listenerList.remove(PopupMenuListener.class,l);
901     }
902
903     /**
904      * Returns an array of all the <code>PopupMenuListener</code>s added
905      * to this JComboBox with addPopupMenuListener().
906      *
907      * @return all of the <code>PopupMenuListener</code>s added or an empty
908      * array if no listeners have been added
909      * @since 1.4
910      */

911     public PopupMenuListener[] getPopupMenuListeners() {
912         return (PopupMenuListener[])listenerList.getListeners(
913                 PopupMenuListener.class);
914     }
915
916     /**
917      * Notifies <code>PopupMenuListener</code>s that the popup portion of the
918      * combo box will become visible.
919      * <p>
920      * This method is public but should not be called by anything other than
921      * the UI delegate.
922      * @see #addPopupMenuListener
923      * @since 1.4
924      */

925     public void firePopupMenuWillBecomeVisible() {
926         Object JavaDoc[] listeners = listenerList.getListenerList();
927         PopupMenuEvent e=null;
928         for (int i = listeners.length-2; i>=0; i-=2) {
929             if (listeners[i]==PopupMenuListener.class) {
930                 if (e == null)
931                     e = new PopupMenuEvent(this);
932                 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
933             }
934         }
935     }
936     
937     /**
938      * Notifies <code>PopupMenuListener</code>s that the popup portion of the
939      * combo box has become invisible.
940      * <p>
941      * This method is public but should not be called by anything other than
942      * the UI delegate.
943      * @see #addPopupMenuListener
944      * @since 1.4
945      */

946     public void firePopupMenuWillBecomeInvisible() {
947         Object JavaDoc[] listeners = listenerList.getListenerList();
948         PopupMenuEvent e=null;
949         for (int i = listeners.length-2; i>=0; i-=2) {
950             if (listeners[i]==PopupMenuListener.class) {
951                 if (e == null)
952                     e = new PopupMenuEvent(this);
953                 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
954             }
955         }
956     }
957     
958     /**
959      * Notifies <code>PopupMenuListener</code>s that the popup portion of the
960      * combo box has been canceled.
961      * <p>
962      * This method is public but should not be called by anything other than
963      * the UI delegate.
964      * @see #addPopupMenuListener
965      * @since 1.4
966      */

967     public void firePopupMenuCanceled() {
968         Object JavaDoc[] listeners = listenerList.getListenerList();
969         PopupMenuEvent e=null;
970         for (int i = listeners.length-2; i>=0; i-=2) {
971             if (listeners[i]==PopupMenuListener.class) {
972                 if (e == null)
973                     e = new PopupMenuEvent(this);
974                 ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
975             }
976         }
977     }
978
979     /**
980      * Sets the action command that should be included in the event
981      * sent to action listeners.
982      *
983      * @param aCommand a string containing the "command" that is sent
984      * to action listeners; the same listener can then
985      * do different things depending on the command it
986      * receives
987      */

988     public void setActionCommand(String JavaDoc aCommand) {
989         actionCommand = aCommand;
990     }
991
992     /**
993      * Returns the action command that is included in the event sent to
994      * action listeners.
995      *
996      * @return the string containing the "command" that is sent
997      * to action listeners.
998      */

999     public String JavaDoc getActionCommand() {
1000        return actionCommand;
1001    }
1002
1003    private Action JavaDoc action;
1004    private PropertyChangeListener actionPropertyChangeListener;
1005
1006    /**
1007     * Sets the <code>Action</code> for the <code>ActionEvent</code> source.
1008     * The new <code>Action</code> replaces any previously set
1009     * <code>Action</code> but does not affect <code>ActionListeners</code>
1010     * independently added with <code>addActionListener</code>.
1011     * If the <code>Action</code> is already a registered
1012     * <code>ActionListener</code> for the <code>ActionEvent</code> source,
1013     * it is not re-registered.
1014     *
1015     * <p>
1016     * A side-effect of setting the <code>Action</code> is that the
1017