1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.beans.PropertyChangeEvent; 28 import java.beans.PropertyChangeListener; 29 import java.beans.Transient; 30 import java.util.*; 31 32 import java.awt.*; 33 import java.awt.event.*; 34 35 import java.io.Serializable; 36 import java.io.ObjectOutputStream; 37 import java.io.IOException; 38 39 import javax.swing.event.*; 40 import javax.swing.plaf.*; 41 42 import javax.accessibility.*; 43 44 /** 45 * A component that combines a button or editable field and a drop-down list. 46 * The user can select a value from the drop-down list, which appears at the 47 * user's request. If you make the combo box editable, then the combo box 48 * includes an editable field into which the user can type a value. 49 * <p> 50 * <strong>Warning:</strong> Swing is not thread safe. For more 51 * information see <a 52 * href="package-summary.html#threading">Swing's Threading 53 * Policy</a>. 54 * <p> 55 * <strong>Warning:</strong> 56 * Serialized objects of this class will not be compatible with 57 * future Swing releases. The current serialization support is 58 * appropriate for short term storage or RMI between applications running 59 * the same version of Swing. As of 1.4, support for long term storage 60 * of all JavaBeans™ 61 * has been added to the <code>java.beans</code> package. 62 * Please see {@link java.beans.XMLEncoder}. 63 * 64 * <p> 65 * See <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html">How to Use Combo Boxes</a> 66 * in <a href="https://docs.oracle.com/javase/tutorial/"><em>The Java Tutorial</em></a> 67 * for further information. 68 * <p> 69 * @see ComboBoxModel 70 * @see DefaultComboBoxModel 71 * 72 * @param <E> the type of the elements of this combo box 73 * 74 * @beaninfo 75 * attribute: isContainer false 76 * description: A combination of a text field and a drop-down list. 77 * 78 * @author Arnaud Weber 79 * @author Mark Davidson 80 */ 81 public class JComboBox<E> extends JComponent 82 implements ItemSelectable,ListDataListener,ActionListener, Accessible { 83 /** 84 * @see #getUIClassID 85 * @see #readObject 86 */ 87 private static final String uiClassID = "ComboBoxUI"; 88 89 /** 90 * This protected field is implementation specific. Do not access directly 91 * or override. Use the accessor methods instead. 92 * 93 * @see #getModel 94 * @see #setModel 95 */ 96 protected ComboBoxModel<E> dataModel; 97 /** 98 * This protected field is implementation specific. Do not access directly 99 * or override. Use the accessor methods instead. 100 * 101 * @see #getRenderer 102 * @see #setRenderer 103 */ 104 protected ListCellRenderer<? super E> renderer; 105 /** 106 * This protected field is implementation specific. Do not access directly 107 * or override. Use the accessor methods instead. 108 * 109 * @see #getEditor 110 * @see #setEditor 111 */ 112 protected ComboBoxEditor editor; 113 /** 114 * This protected field is implementation specific. Do not access directly 115 * or override. Use the accessor methods instead. 116 * 117 * @see #getMaximumRowCount 118 * @see #setMaximumRowCount 119 */ 120 protected int maximumRowCount = 8; 121 122 /** 123 * This protected field is implementation specific. Do not access directly 124 * or override. Use the accessor methods instead. 125 * 126 * @see #isEditable 127 * @see #setEditable 128 */ 129 protected boolean isEditable = false; 130 /** 131 * This protected field is implementation specific. Do not access directly 132 * or override. Use the accessor methods instead. 133 * 134 * @see #setKeySelectionManager 135 * @see #getKeySelectionManager 136 */ 137 protected KeySelectionManager keySelectionManager = null; 138 /** 139 * This protected field is implementation specific. Do not access directly 140 * or override. Use the accessor methods instead. 141 * 142 * @see #setActionCommand 143 * @see #getActionCommand 144 */ 145 protected String actionCommand = "comboBoxChanged"; 146 /** 147 * This protected field is implementation specific. Do not access directly 148 * or override. Use the accessor methods instead. 149 * 150 * @see #setLightWeightPopupEnabled 151 * @see #isLightWeightPopupEnabled 152 */ 153 protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled(); 154 155 /** 156 * This protected field is implementation specific. Do not access directly 157 * or override. 158 */ 159 protected Object selectedItemReminder = null; 160 161 private E prototypeDisplayValue; 162 163 // Flag to ensure that infinite loops do not occur with ActionEvents. 164 private boolean firingActionEvent = false; 165 166 // Flag to ensure the we don't get multiple ActionEvents on item selection. 167 private boolean selectingItem = false; 168 169 /** 170 * Creates a <code>JComboBox</code> that takes its items from an 171 * existing <code>ComboBoxModel</code>. Since the 172 * <code>ComboBoxModel</code> is provided, a combo box created using 173 * this constructor does not create a default combo box model and 174 * may impact how the insert, remove and add methods behave. 175 * 176 * @param aModel the <code>ComboBoxModel</code> that provides the 177 * displayed list of items 178 * @see DefaultComboBoxModel 179 */ 180 public JComboBox(ComboBoxModel<E> aModel) { 181 super(); 182 setModel(aModel); 183 init(); 184 } 185 186 /** 187 * Creates a <code>JComboBox</code> that contains the elements 188 * in the specified array. By default the first item in the array 189 * (and therefore the data model) becomes selected. 190 * 191 * @param items an array of objects to insert into the combo box 192 * @see DefaultComboBoxModel 193 */ 194 public JComboBox(E[] items) { 195 super(); 196 setModel(new DefaultComboBoxModel<E>(items)); 197 init(); 198 } 199 200 /** 201 * Creates a <code>JComboBox</code> that contains the elements 202 * in the specified Vector. By default the first item in the vector 203 * (and therefore the data model) becomes selected. 204 * 205 * @param items an array of vectors to insert into the combo box 206 * @see DefaultComboBoxModel 207 */ 208 public JComboBox(Vector<E> items) { 209 super(); 210 setModel(new DefaultComboBoxModel<E>(items)); 211 init(); 212 } 213 214 /** 215 * Creates a <code>JComboBox</code> with a default data model. 216 * The default data model is an empty list of objects. 217 * Use <code>addItem</code> to add items. By default the first item 218 * in the data model becomes selected. 219 * 220 * @see DefaultComboBoxModel 221 */ 222 public JComboBox() { 223 super(); 224 setModel(new DefaultComboBoxModel<E>()); 225 init(); 226 } 227 228 private void init() { 229 installAncestorListener(); 230 setUIProperty("opaque",true); 231 updateUI(); 232 } 233 234 protected void installAncestorListener() { 235 addAncestorListener(new AncestorListener(){ 236 public void ancestorAdded(AncestorEvent event){ hidePopup();} 237 public void ancestorRemoved(AncestorEvent event){ hidePopup();} 238 public void ancestorMoved(AncestorEvent event){ 239 if (event.getSource() != JComboBox.this) 240 hidePopup(); 241 }}); 242 } 243 244 /** 245 * Sets the L&F object that renders this component. 246 * 247 * @param ui the <code>ComboBoxUI</code> L&F object 248 * @see UIDefaults#getUI 249 * 250 * @beaninfo 251 * bound: true 252 * hidden: true 253 * attribute: visualUpdate true 254 * description: The UI object that implements the Component's LookAndFeel. 255 */ 256 public void setUI(ComboBoxUI ui) { 257 super.setUI(ui); 258 } 259 260 /** 261 * Resets the UI property to a value from the current look and feel. 262 * 263 * @see JComponent#updateUI 264 */ 265 public void updateUI() { 266 setUI((ComboBoxUI)UIManager.getUI(this)); 267 268 ListCellRenderer<? super E> renderer = getRenderer(); 269 if (renderer instanceof Component) { 270 SwingUtilities.updateComponentTreeUI((Component)renderer); 271 } 272 } 273 274 275 /** 276 * Returns the name of the L&F class that renders this component. 277 * 278 * @return the string "ComboBoxUI" 279 * @see JComponent#getUIClassID 280 * @see UIDefaults#getUI 281 */ 282 public String getUIClassID() { 283 return uiClassID; 284 } 285 286 287 /** 288 * Returns the L&F object that renders this component. 289 * 290 * @return the ComboBoxUI object that renders this component 291 */ 292 public ComboBoxUI getUI() { 293 return(ComboBoxUI)ui; 294 } 295 296 /** 297 * Sets the data model that the <code>JComboBox</code> uses to obtain 298 * the list of items. 299 * 300 * @param aModel the <code>ComboBoxModel</code> that provides the 301 * displayed list of items 302 * 303 * @beaninfo 304 * bound: true 305 * description: Model that the combo box uses to get data to display. 306 */ 307 public void setModel(ComboBoxModel<E> aModel) { 308 ComboBoxModel<E> oldModel = dataModel; 309 if (oldModel != null) { 310 oldModel.removeListDataListener(this); 311 } 312 dataModel = aModel; 313 dataModel.addListDataListener(this); 314 315 // set the current selected item. 316 selectedItemReminder = dataModel.getSelectedItem(); 317 318 firePropertyChange( "model", oldModel, dataModel); 319 } 320 321 /** 322 * Returns the data model currently used by the <code>JComboBox</code>. 323 * 324 * @return the <code>ComboBoxModel</code> that provides the displayed 325 * list of items 326 */ 327 public ComboBoxModel<E> getModel() { 328 return dataModel; 329 } 330 331 /* 332 * Properties 333 */ 334 335 /** 336 * Sets the <code>lightWeightPopupEnabled</code> property, which 337 * provides a hint as to whether or not a lightweight 338 * <code>Component</code> should be used to contain the 339 * <code>JComboBox</code>, versus a heavyweight 340 * <code>Component</code> such as a <code>Panel</code> 341 * or a <code>Window</code>. The decision of lightweight 342 * versus heavyweight is ultimately up to the 343 * <code>JComboBox</code>. Lightweight windows are more 344 * efficient than heavyweight windows, but lightweight 345 * and heavyweight components do not mix well in a GUI. 346 * If your application mixes lightweight and heavyweight 347 * components, you should disable lightweight popups. 348 * The default value for the <code>lightWeightPopupEnabled</code> 349 * property is <code>true</code>, unless otherwise specified 350 * by the look and feel. Some look and feels always use 351 * heavyweight popups, no matter what the value of this property. 352 * <p> 353 * See the article <a href="http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html">Mixing Heavy and Light Components</a> 354 * This method fires a property changed event. 355 * 356 * @param aFlag if <code>true</code>, lightweight popups are desired 357 * 358 * @beaninfo 359 * bound: true 360 * expert: true 361 * description: Set to <code>false</code> to require heavyweight popups. 362 */ 363 public void setLightWeightPopupEnabled(boolean aFlag) { 364 boolean oldFlag = lightWeightPopupEnabled; 365 lightWeightPopupEnabled = aFlag; 366 firePropertyChange("lightWeightPopupEnabled", oldFlag, lightWeightPopupEnabled); 367 } 368 369 /** 370 * Gets the value of the <code>lightWeightPopupEnabled</code> 371 * property. 372 * 373 * @return the value of the <code>lightWeightPopupEnabled</code> 374 * property 375 * @see #setLightWeightPopupEnabled 376 */ 377 public boolean isLightWeightPopupEnabled() { 378 return lightWeightPopupEnabled; 379 } 380 381 /** 382 * Determines whether the <code>JComboBox</code> field is editable. 383 * An editable <code>JComboBox</code> allows the user to type into the 384 * field or selected an item from the list to initialize the field, 385 * after which it can be edited. (The editing affects only the field, 386 * the list item remains intact.) A non editable <code>JComboBox</code> 387 * displays the selected item in the field, 388 * but the selection cannot be modified. 389 * 390 * @param aFlag a boolean value, where true indicates that the 391 * field is editable 392 * 393 * @beaninfo 394 * bound: true 395 * preferred: true 396 * description: If true, the user can type a new value in the combo box. 397 */ 398 public void setEditable(boolean aFlag) { 399 boolean oldFlag = isEditable; 400 isEditable = aFlag; 401 firePropertyChange( "editable", oldFlag, isEditable ); 402 } 403 404 /** 405 * Returns true if the <code>JComboBox</code> is editable. 406 * By default, a combo box is not editable. 407 * 408 * @return true if the <code>JComboBox</code> is editable, else false 409 */ 410 public boolean isEditable() { 411 return isEditable; 412 } 413 414 /** 415 * Sets the maximum number of rows the <code>JComboBox</code> displays. 416 * If the number of objects in the model is greater than count, 417 * the combo box uses a scrollbar. 418 * 419 * @param count an integer specifying the maximum number of items to 420 * display in the list before using a scrollbar 421 * @beaninfo 422 * bound: true 423 * preferred: true 424 * description: The maximum number of rows the popup should have 425 */ 426 public void setMaximumRowCount(int count) { 427 int oldCount = maximumRowCount; 428 maximumRowCount = count; 429 firePropertyChange( "maximumRowCount", oldCount, maximumRowCount ); 430 } 431 432 /** 433 * Returns the maximum number of items the combo box can display 434 * without a scrollbar 435 * 436 * @return an integer specifying the maximum number of items that are 437 * displayed in the list before using a scrollbar 438 */ 439 public int getMaximumRowCount() { 440 return maximumRowCount; 441 } 442 443 /** 444 * Sets the renderer that paints the list items and the item selected from the list in 445 * the JComboBox field. The renderer is used if the JComboBox is not 446 * editable. If it is editable, the editor is used to render and edit 447 * the selected item. 448 * <p> 449 * The default renderer displays a string or an icon. 450 * Other renderers can handle graphic images and composite items. 451 * <p> 452 * To display the selected item, 453 * <code>aRenderer.getListCellRendererComponent</code> 454 * is called, passing the list object and an index of -1. 455 * 456 * @param aRenderer the <code>ListCellRenderer</code> that 457 * displays the selected item 458 * @see #setEditor 459 * @beaninfo 460 * bound: true 461 * expert: true 462 * description: The renderer that paints the item selected in the list. 463 */ 464 public void setRenderer(ListCellRenderer<? super E> aRenderer) { 465 ListCellRenderer<? super E> oldRenderer = renderer; 466 renderer = aRenderer; 467 firePropertyChange( "renderer", oldRenderer, renderer ); 468 invalidate(); 469 } 470 471 /** 472 * Returns the renderer used to display the selected item in the 473 * <code>JComboBox</code> field. 474 * 475 * @return the <code>ListCellRenderer</code> that displays 476 * the selected item. 477 */ 478 public ListCellRenderer<? super E> getRenderer() { 479 return renderer; 480 } 481 482 /** 483 * Sets the editor used to paint and edit the selected item in the 484 * <code>JComboBox</code> field. The editor is used only if the 485 * receiving <code>JComboBox</code> is editable. If not editable, 486 * the combo box uses the renderer to paint the selected item. 487 * 488 * @param anEditor the <code>ComboBoxEditor</code> that 489 * displays the selected item 490 * @see #setRenderer 491 * @beaninfo 492 * bound: true 493 * expert: true 494 * description: The editor that combo box uses to edit the current value 495 */ 496 public void setEditor(ComboBoxEditor anEditor) { 497 ComboBoxEditor oldEditor = editor; 498 499 if ( editor != null ) { 500 editor.removeActionListener(this); 501 } 502 editor = anEditor; 503 if ( editor != null ) { 504 editor.addActionListener(this); 505 } 506 firePropertyChange( "editor", oldEditor, editor ); 507 } 508 509 /** 510 * Returns the editor used to paint and edit the selected item in the 511 * <code>JComboBox</code> field. 512 * 513 * @return the <code>ComboBoxEditor</code> that displays the selected item 514 */ 515 public ComboBoxEditor getEditor() { 516 return editor; 517 } 518 519 // 520 // Selection 521 // 522 523 /** 524 * Sets the selected item in the combo box display area to the object in 525 * the argument. 526 * If <code>anObject</code> is in the list, the display area shows 527 * <code>anObject</code> selected. 528 * <p> 529 * If <code>anObject</code> is <i>not</i> in the list and the combo box is 530 * uneditable, it will not change the current selection. For editable 531 * combo boxes, the selection will change to <code>anObject</code>. 532 * <p> 533 * If this constitutes a change in the selected item, 534 * <code>ItemListener</code>s added to the combo box will be notified with 535 * one or two <code>ItemEvent</code>s. 536 * If there is a current selected item, an <code>ItemEvent</code> will be 537 * fired and the state change will be <code>ItemEvent.DESELECTED</code>. 538 * If <code>anObject</code> is in the list and is not currently selected 539 * then an <code>ItemEvent</code> will be fired and the state change will 540 * be <code>ItemEvent.SELECTED</code>. 541 * <p> 542 * <code>ActionListener</code>s added to the combo box will be notified 543 * with an <code>ActionEvent</code> when this method is called. 544 * 545 * @param anObject the list object to select; use <code>null</code> to 546 clear the selection 547 * @beaninfo 548 * preferred: true 549 * description: Sets the selected item in the JComboBox. 550 */ 551 public void setSelectedItem(Object anObject) { 552 Object oldSelection = selectedItemReminder; 553 Object objectToSelect = anObject; 554 if (oldSelection == null || !oldSelection.equals(anObject)) { 555 556 if (anObject != null && !isEditable()) { 557 // For non editable combo boxes, an invalid selection 558 // will be rejected. 559 boolean found = false; 560 for (int i = 0; i < dataModel.getSize(); i++) { 561 E element = dataModel.getElementAt(i); 562 if (anObject.equals(element)) { 563 found = true; 564 objectToSelect = element; 565 break; 566 } 567 } 568 if (!found) { 569 return; 570 } 571 572 getEditor().setItem(anObject); 573 } 574 575 // Must toggle the state of this flag since this method 576 // call may result in ListDataEvents being fired. 577 selectingItem = true; 578 dataModel.setSelectedItem(objectToSelect); 579 selectingItem = false; 580 581 if (selectedItemReminder != dataModel.getSelectedItem()) { 582 // in case a users implementation of ComboBoxModel 583 // doesn't fire a ListDataEvent when the selection 584 // changes. 585 selectedItemChanged(); 586 } 587 } 588 fireActionEvent(); 589 } 590 591 /** 592 * Returns the current selected item. 593 * <p> 594 * If the combo box is editable, then this value may not have been added 595 * to the combo box with <code>addItem</code>, <code>insertItemAt</code> 596 * or the data constructors. 597 * 598 * @return the current selected Object 599 * @see #setSelectedItem 600 */ 601 public Object getSelectedItem() { 602 return dataModel.getSelectedItem(); 603 } 604 605 /** 606 * Selects the item at index <code>anIndex</code>. 607 * 608 * @param anIndex an integer specifying the list item to select, 609 * where 0 specifies the first item in the list and -1 indicates no selection 610 * @exception IllegalArgumentException if <code>anIndex</code> < -1 or 611 * <code>anIndex</code> is greater than or equal to size 612 * @beaninfo 613 * preferred: true 614 * description: The item at index is selected. 615 */ 616 public void setSelectedIndex(int anIndex) { 617 int size = dataModel.getSize(); 618 619 if ( anIndex == -1 ) { 620 setSelectedItem( null ); 621 } else if ( anIndex < -1 || anIndex >= size ) { 622 throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds"); 623 } else { 624 setSelectedItem(dataModel.getElementAt(anIndex)); 625 } 626 } 627 628 /** 629 * Returns the first item in the list that matches the given item. 630 * The result is not always defined if the <code>JComboBox</code> 631 * allows selected items that are not in the list. 632 * Returns -1 if there is no selected item or if the user specified 633 * an item which is not in the list. 634 635 * @return an integer specifying the currently selected list item, 636 * where 0 specifies 637 * the first item in the list; 638 * or -1 if no item is selected or if 639 * the currently selected item is not in the list 640 */ 641 @Transient 642 public int getSelectedIndex() { 643 Object sObject = dataModel.getSelectedItem(); 644 int i,c; 645 E obj; 646 647 for ( i=0,c=dataModel.getSize();i<c;i++ ) { 648 obj = dataModel.getElementAt(i); 649 if ( obj != null && obj.equals(sObject) ) 650 return i; 651 } 652 return -1; 653 } 654 655 /** 656 * Returns the "prototypical display" value - an Object used 657 * for the calculation of the display height and width. 658 * 659 * @return the value of the <code>prototypeDisplayValue</code> property 660 * @see #setPrototypeDisplayValue 661 * @since 1.4 662 */ 663 public E getPrototypeDisplayValue() { 664 return prototypeDisplayValue; 665 } 666 667 /** 668 * Sets the prototype display value used to calculate the size of the display 669 * for the UI portion. 670 * <p> 671 * If a prototype display value is specified, the preferred size of 672 * the combo box is calculated by configuring the renderer with the 673 * prototype display value and obtaining its preferred size. Specifying 674 * the preferred display value is often useful when the combo box will be 675 * displaying large amounts of data. If no prototype display value has 676 * been specified, the renderer must be configured for each value from 677 * the model and its preferred size obtained, which can be 678 * relatively expensive. 679 * 680 * @param prototypeDisplayValue 681 * @see #getPrototypeDisplayValue 682 * @since 1.4 683 * @beaninfo 684 * bound: true 685 * attribute: visualUpdate true 686 * description: The display prototype value, used to compute display width and height. 687 */ 688 public void setPrototypeDisplayValue(E prototypeDisplayValue) { 689 Object oldValue = this.prototypeDisplayValue; 690 this.prototypeDisplayValue = prototypeDisplayValue; 691 firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue); 692 } 693 694 /** 695 * Adds an item to the item list. 696 * This method works only if the <code>JComboBox</code> uses a 697 * mutable data model. 698 * <p> 699 * <strong>Warning:</strong> 700 * Focus and keyboard navigation problems may arise if you add duplicate 701 * String objects. A workaround is to add new objects instead of String 702 * objects and make sure that the toString() method is defined. 703 * For example: 704 * <pre> 705 * comboBox.addItem(makeObj("Item 1")); 706 * comboBox.addItem(makeObj("Item 1")); 707 * ... 708 * private Object makeObj(final String item) { 709 * return new Object() { public String toString() { return item; } }; 710 * } 711 * </pre> 712 * 713 * @param item the item to add to the list 714 * @see MutableComboBoxModel 715 */ 716 public void addItem(E item) { 717 checkMutableComboBoxModel(); 718 ((MutableComboBoxModel<E>)dataModel).addElement(item); 719 } 720 721 /** 722 * Inserts an item into the item list at a given index. 723 * This method works only if the <code>JComboBox</code> uses a 724 * mutable data model. 725 * 726 * @param item the item to add to the list 727 * @param index an integer specifying the position at which 728 * to add the item 729 * @see MutableComboBoxModel 730 */ 731 public void insertItemAt(E item, int index) { 732 checkMutableComboBoxModel(); 733 ((MutableComboBoxModel<E>)dataModel).insertElementAt(item,index); 734 } 735 736 /** 737 * Removes an item from the item list. 738 * This method works only if the <code>JComboBox</code> uses a 739 * mutable data model. 740 * 741 * @param anObject the object to remove from the item list 742 * @see MutableComboBoxModel 743 */ 744 public void removeItem(Object anObject) { 745 checkMutableComboBoxModel(); 746 ((MutableComboBoxModel)dataModel).removeElement(anObject); 747 } 748 749 /** 750 * Removes the item at <code>anIndex</code> 751 * This method works only if the <code>JComboBox</code> uses a 752 * mutable data model. 753 * 754 * @param anIndex an int specifying the index of the item to remove, 755 * where 0 756 * indicates the first item in the list 757 * @see MutableComboBoxModel 758 */ 759 public void removeItemAt(int anIndex) { 760 checkMutableComboBoxModel(); 761 ((MutableComboBoxModel<E>)dataModel).removeElementAt( anIndex ); 762 } 763 764 /** 765 * Removes all items from the item list. 766 */ 767 public void removeAllItems() { 768 checkMutableComboBoxModel(); 769 MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel; 770 int size = model.getSize(); 771 772 if ( model instanceof DefaultComboBoxModel ) { 773 ((DefaultComboBoxModel)model).removeAllElements(); 774 } 775 else { 776 for ( int i = 0; i < size; ++i ) { 777 E element = model.getElementAt( 0 ); 778 model.removeElement( element ); 779 } 780 } 781 selectedItemReminder = null; 782 if (isEditable()) { 783 editor.setItem(null); 784 } 785 } 786 787 /** 788 * Checks that the <code>dataModel</code> is an instance of 789 * <code>MutableComboBoxModel</code>. If not, it throws an exception. 790 * @exception RuntimeException if <code>dataModel</code> is not an 791 * instance of <code>MutableComboBoxModel</code>. 792 */ 793 void checkMutableComboBoxModel() { 794 if ( !(dataModel instanceof MutableComboBoxModel) ) 795 throw new RuntimeException("Cannot use this method with a non-Mutable data model."); 796 } 797 798 /** 799 * Causes the combo box to display its popup window. 800 * @see #setPopupVisible 801 */ 802 public void showPopup() { 803 setPopupVisible(true); 804 } 805 806 /** 807 * Causes the combo box to close its popup window. 808 * @see #setPopupVisible 809 */ 810 public void hidePopup() { 811 setPopupVisible(false); 812 } 813 814 /** 815 * Sets the visibility of the popup. 816 */ 817 public void setPopupVisible(boolean v) { 818 getUI().setPopupVisible(this, v); 819 } 820 821 /** 822 * Determines the visibility of the popup. 823 * 824 * @return true if the popup is visible, otherwise returns false 825 */ 826 public boolean isPopupVisible() { 827 return getUI().isPopupVisible(this); 828 } 829 830 /** Selection **/ 831 832 /** 833 * Adds an <code>ItemListener</code>. 834 * <p> 835 * <code>aListener</code> will receive one or two <code>ItemEvent</code>s when 836 * the selected item changes. 837 * 838 * @param aListener the <code>ItemListener</code> that is to be notified 839 * @see #setSelectedItem 840 */ 841 public void addItemListener(ItemListener aListener) { 842 listenerList.add(ItemListener.class,aListener); 843 } 844 845 /** Removes an <code>ItemListener</code>. 846 * 847 * @param aListener the <code>ItemListener</code> to remove 848 */ 849 public void removeItemListener(ItemListener aListener) { 850 listenerList.remove(ItemListener.class,aListener); 851 } 852 853 /** 854 * Returns an array of all the <code>ItemListener</code>s added 855 * to this JComboBox with addItemListener(). 856 * 857 * @return all of the <code>ItemListener</code>s added or an empty 858 * array if no listeners have been added 859 * @since 1.4 860 */ 861 public ItemListener[] getItemListeners() { 862 return listenerList.getListeners(ItemListener.class); 863 } 864 865 /** 866 * Adds an <code>ActionListener</code>. 867 * <p> 868 * The <code>ActionListener</code> will receive an <code>ActionEvent</code> 869 * when a selection has been made. If the combo box is editable, then 870 * an <code>ActionEvent</code> will be fired when editing has stopped. 871 * 872 * @param l the <code>ActionListener</code> that is to be notified 873 * @see #setSelectedItem 874 */ 875 public void addActionListener(ActionListener l) { 876 listenerList.add(ActionListener.class,l); 877 } 878 879 /** Removes an <code>ActionListener</code>. 880 * 881 * @param l the <code>ActionListener</code> to remove 882 */ 883 public void removeActionListener(ActionListener l) { 884 if ((l != null) && (getAction() == l)) { 885 setAction(null); 886 } else { 887 listenerList.remove(ActionListener.class, l); 888 } 889 } 890 891 /** 892 * Returns an array of all the <code>ActionListener</code>s added 893 * to this JComboBox with addActionListener(). 894 * 895 * @return all of the <code>ActionListener</code>s added or an empty 896 * array if no listeners have been added 897 * @since 1.4 898 */ 899 public ActionListener[] getActionListeners() { 900 return listenerList.getListeners(ActionListener.class); 901 } 902 903 /** 904 * Adds a <code>PopupMenu</code> listener which will listen to notification 905 * messages from the popup portion of the combo box. 906 * <p> 907 * For all standard look and feels shipped with Java, the popup list 908 * portion of combo box is implemented as a <code>JPopupMenu</code>. 909 * A custom look and feel may not implement it this way and will 910 * therefore not receive the notification. 911 * 912 * @param l the <code>PopupMenuListener</code> to add 913 * @since 1.4 914 */ 915 public void addPopupMenuListener(PopupMenuListener l) { 916 listenerList.add(PopupMenuListener.class,l); 917 } 918 919 /** 920 * Removes a <code>PopupMenuListener</code>. 921 * 922 * @param l the <code>PopupMenuListener</code> to remove 923 * @see #addPopupMenuListener 924 * @since 1.4 925 */ 926 public void removePopupMenuListener(PopupMenuListener l) { 927 listenerList.remove(PopupMenuListener.class,l); 928 } 929 930 /** 931 * Returns an array of all the <code>PopupMenuListener</code>s added 932 * to this JComboBox with addPopupMenuListener(). 933 * 934 * @return all of the <code>PopupMenuListener</code>s added or an empty 935 * array if no listeners have been added 936 * @since 1.4 937 */ 938 public PopupMenuListener[] getPopupMenuListeners() { 939 return listenerList.getListeners(PopupMenuListener.class); 940 } 941 942 /** 943 * Notifies <code>PopupMenuListener</code>s that the popup portion of the 944 * combo box will become visible. 945 * <p> 946 * This method is public but should not be called by anything other than 947 * the UI delegate. 948 * @see #addPopupMenuListener 949 * @since 1.4 950 */ 951 public void firePopupMenuWillBecomeVisible() { 952 Object[] listeners = listenerList.getListenerList(); 953 PopupMenuEvent e=null; 954 for (int i = listeners.length-2; i>=0; i-=2) { 955 if (listeners[i]==PopupMenuListener.class) { 956 if (e == null) 957 e = new PopupMenuEvent(this); 958 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e); 959 } 960 } 961 } 962 963 /** 964 * Notifies <code>PopupMenuListener</code>s that the popup portion of the 965 * combo box has become invisible. 966 * <p> 967 * This method is public but should not be called by anything other than 968 * the UI delegate. 969 * @see #addPopupMenuListener 970 * @since 1.4 971 */ 972 public void firePopupMenuWillBecomeInvisible() { 973 Object[] listeners = listenerList.getListenerList(); 974 PopupMenuEvent e=null; 975 for (int i = listeners.length-2; i>=0; i-=2) { 976 if (listeners[i]==PopupMenuListener.class) { 977 if (e == null) 978 e = new PopupMenuEvent(this); 979 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e); 980 } 981 } 982 } 983 984 /** 985 * Notifies <code>PopupMenuListener</code>s that the popup portion of the 986 * combo box has been canceled. 987 * <p> 988 * This method is public but should not be called by anything other than 989 * the UI delegate. 990 * @see #addPopupMenuListener 991 * @since 1.4 992 */ 993 public void firePopupMenuCanceled() { 994 Object[] listeners = listenerList.getListenerList(); 995 PopupMenuEvent e=null; 996 for (int i = listeners.length-2; i>=0; i-=2) { 997 if (listeners[i]==PopupMenuListener.class) { 998 if (e == null) 999 e = new PopupMenuEvent(this); 1000 ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e); 1001 } 1002 } 1003 } 1004 1005 /** 1006 * Sets the action command that should be included in the event 1007 * sent to action listeners. 1008 * 1009 * @param aCommand a string containing the "command" that is sent 1010 * to action listeners; the same listener can then 1011 * do different things depending on the command it 1012 * receives 1013 */ 1014 public void setActionCommand(String aCommand) { 1015 actionCommand = aCommand; 1016 } 1017 1018 /** 1019 * Returns the action command that is included in the event sent to 1020 * action listeners. 1021 * 1022 * @return the string containing the "command" that is sent 1023 * to action listeners. 1024 */ 1025 public String getActionCommand() { 1026 return actionCommand; 1027 } 1028 1029 private Action action; 1030 private PropertyChangeListener actionPropertyChangeListener; 1031 1032 /** 1033 * Sets the <code>Action</code> for the <code>ActionEvent</code> source. 1034 * The new <code>Action</code> replaces any previously set 1035 * <code>Action</code> but does not affect <code>ActionListeners</code> 1036 * independently added with <code>addActionListener</code>. 1037 * If the <code>Action</code> is already a registered 1038 * <code>ActionListener</code> for the <code>ActionEvent</code> source, 1039 * it is not re-registered. 1040 * <p> 1041 * Setting the <code>Action</code> results in immediately changing 1042 * all the properties described in <a href="Action.html#buttonActions"> 1043 * Swing Components Supporting <code>Action</code></a>. 1044 * Subsequently, the combobox's properties are automatically updated 1045 * as the <code>Action</code>'s properties change. 1046 * <p> 1047 * This method uses three other methods to set 1048 * and help track the <code>Action</code>'s property values. 1049 * It uses the <code>configurePropertiesFromAction</code> method 1050 * to immediately change the combobox's properties. 1051 * To track changes in the <code>Action</code>'s property values, 1052 * this method registers the <code>PropertyChangeListener</code> 1053 * returned by <code>createActionPropertyChangeListener</code>. The 1054 * default {@code PropertyChangeListener} invokes the 1055 * {@code actionPropertyChanged} method when a property in the 1056 * {@code Action} changes. 1057 * 1058 * @param a the <code>Action</code> for the <code>JComboBox</code>, 1059 * or <code>null</code>. 1060 * @since 1.3 1061 * @see Action 1062 * @see #getAction 1063 * @see #configurePropertiesFromAction 1064 * @see #createActionPropertyChangeListener 1065 * @see #actionPropertyChanged 1066 * @beaninfo 1067 * bound: true 1068 * attribute: visualUpdate true 1069 * description: the Action instance connected with this ActionEvent source 1070 */ 1071 public void setAction(Action a) { 1072 Action oldValue = getAction(); 1073 if (action==null || !action.equals(a)) { 1074 action = a; 1075 if (oldValue!=null) { 1076 removeActionListener(oldValue); 1077 oldValue.removePropertyChangeListener(actionPropertyChangeListener); 1078 actionPropertyChangeListener = null; 1079 } 1080 configurePropertiesFromAction(action); 1081 if (action!=null) { 1082 // Don't add if it is already a listener 1083 if (!isListener(ActionListener.class, action)) { 1084 addActionListener(action); 1085 } 1086 // Reverse linkage: 1087 actionPropertyChangeListener = createActionPropertyChangeListener(action); 1088 action.addPropertyChangeListener(actionPropertyChangeListener); 1089 } 1090 firePropertyChange("action", oldValue, action); 1091 } 1092 } 1093 1094 private boolean isListener(Class c, ActionListener a) { 1095 boolean isListener = false; 1096 Object[] listeners = listenerList.getListenerList(); 1097 for (int i = listeners.length-2; i>=0; i-=2) { 1098 if (listeners[i]==c && listeners[i+1]==a) { 1099 isListener=true; 1100 } 1101 } 1102 return isListener; 1103 } 1104 1105 /** 1106 * Returns the currently set <code>Action</code> for this 1107 * <code>ActionEvent</code> source, or <code>null</code> if no 1108 * <code>Action</code> is set. 1109 * 1110 * @return the <code>Action</code> for this <code>ActionEvent</code> 1111 * source; or <code>null</code> 1112 * @since 1.3 1113 * @see Action 1114 * @see #setAction 1115 */ 1116 public Action getAction() { 1117 return action; 1118 } 1119 1120 /** 1121 * Sets the properties on this combobox to match those in the specified 1122 * <code>Action</code>. Refer to <a href="Action.html#buttonActions"> 1123 * Swing Components Supporting <code>Action</code></a> for more 1124 * details as to which properties this sets. 1125 * 1126 * @param a the <code>Action</code> from which to get the properties, 1127 * or <code>null</code> 1128 * @since 1.3 1129 * @see Action 1130 * @see #setAction 1131 */ 1132 protected void configurePropertiesFromAction(Action a) { 1133 AbstractAction.setEnabledFromAction(this, a); 1134 AbstractAction.setToolTipTextFromAction(this, a); 1135 setActionCommandFromAction(a); 1136 } 1137 1138 /** 1139 * Creates and returns a <code>PropertyChangeListener</code> that is 1140 * responsible for listening for changes from the specified 1141 * <code>Action</code> and updating the appropriate properties. 1142 * <p> 1143 * <b>Warning:</b> If you subclass this do not create an anonymous 1144 * inner class. If you do the lifetime of the combobox will be tied to 1145 * that of the <code>Action</code>. 1146 * 1147 * @param a the combobox's action 1148 * @since 1.3 1149 * @see Action 1150 * @see #setAction 1151 */ 1152 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 1153 return new ComboBoxActionPropertyChangeListener(this, a); 1154 } 1155 1156 /** 1157 * Updates the combobox's state in response to property changes in 1158 * associated action. This method is invoked from the 1159 * {@code PropertyChangeListener} returned from 1160 * {@code createActionPropertyChangeListener}. Subclasses do not normally 1161 * need to invoke this. Subclasses that support additional {@code Action} 1162 * properties should override this and 1163 * {@code configurePropertiesFromAction}. 1164 * <p> 1165 * Refer to the table at <a href="Action.html#buttonActions"> 1166 * Swing Components Supporting <code>Action</code></a> for a list of 1167 * the properties this method sets. 1168 * 1169 * @param action the <code>Action</code> associated with this combobox 1170 * @param propertyName the name of the property that changed 1171 * @since 1.6 1172 * @see Action 1173 * @see #configurePropertiesFromAction 1174 */ 1175 protected void actionPropertyChanged(Action action, String propertyName) { 1176 if (propertyName == Action.ACTION_COMMAND_KEY) { 1177 setActionCommandFromAction(action); 1178 } else if (propertyName == "enabled") { 1179 AbstractAction.setEnabledFromAction(this, action); 1180 } else if (Action.SHORT_DESCRIPTION == propertyName) { 1181 AbstractAction.setToolTipTextFromAction(this, action); 1182 } 1183 } 1184 1185 private void setActionCommandFromAction(Action a) { 1186 setActionCommand((a != null) ? 1187 (String)a.getValue(Action.ACTION_COMMAND_KEY) : 1188 null); 1189 } 1190 1191 1192 private static class ComboBoxActionPropertyChangeListener 1193 extends ActionPropertyChangeListener<JComboBox<?>> { 1194 ComboBoxActionPropertyChangeListener(JComboBox<?> b, Action a) { 1195 super(b, a); 1196 } 1197 protected void actionPropertyChanged(JComboBox<?> cb, 1198 Action action, 1199 PropertyChangeEvent e) { 1200 if (AbstractAction.shouldReconfigure(e)) { 1201 cb.configurePropertiesFromAction(action); 1202 } else { 1203 cb.actionPropertyChanged(action, e.getPropertyName()); 1204 } 1205 } 1206 } 1207 1208 /** 1209 * Notifies all listeners that have registered interest for 1210 * notification on this event type. 1211 * @param e the event of interest 1212 * 1213 * @see EventListenerList 1214 */ 1215 protected void fireItemStateChanged(ItemEvent e) { 1216 // Guaranteed to return a non-null array 1217 Object[] listeners = listenerList.getListenerList(); 1218 // Process the listeners last to first, notifying 1219 // those that are interested in this event 1220 for ( int i = listeners.length-2; i>=0; i-=2 ) { 1221 if ( listeners[i]==ItemListener.class ) { 1222 // Lazily create the event: 1223 // if (changeEvent == null) 1224 // changeEvent = new ChangeEvent(this); 1225 ((ItemListener)listeners[i+1]).itemStateChanged(e); 1226 } 1227 } 1228 } 1229 1230 /** 1231 * Notifies all listeners that have registered interest for 1232 * notification on this event type. 1233 * 1234 * @see EventListenerList 1235 */ 1236 protected void fireActionEvent() { 1237 if (!firingActionEvent) { 1238 // Set flag to ensure that an infinite loop is not created 1239 firingActionEvent = true; 1240 ActionEvent e = null; 1241 // Guaranteed to return a non-null array 1242 Object[] listeners = listenerList.getListenerList(); 1243 long mostRecentEventTime = EventQueue.getMostRecentEventTime(); 1244 int modifiers = 0; 1245 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 1246 if (currentEvent instanceof InputEvent) { 1247 modifiers = ((InputEvent)currentEvent).getModifiers(); 1248 } else if (currentEvent instanceof ActionEvent) { 1249 modifiers = ((ActionEvent)currentEvent).getModifiers(); 1250 } 1251 // Process the listeners last to first, notifying 1252 // those that are interested in this event 1253 for ( int i = listeners.length-2; i>=0; i-=2 ) { 1254 if ( listeners[i]==ActionListener.class ) { 1255 // Lazily create the event: 1256 if ( e == null ) 1257 e = new ActionEvent(this,ActionEvent.ACTION_PERFORMED, 1258 getActionCommand(), 1259 mostRecentEventTime, modifiers); 1260 ((ActionListener)listeners[i+1]).actionPerformed(e); 1261 } 1262 } 1263 firingActionEvent = false; 1264 } 1265 } 1266 1267 /** 1268 * This protected method is implementation specific. Do not access directly 1269 * or override. 1270 */ 1271 protected void selectedItemChanged() { 1272 if (selectedItemReminder != null ) { 1273 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED, 1274 selectedItemReminder, 1275 ItemEvent.DESELECTED)); 1276 } 1277 1278 // set the new selected item. 1279 selectedItemReminder = dataModel.getSelectedItem(); 1280 1281 if (selectedItemReminder != null ) { 1282 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED, 1283 selectedItemReminder, 1284 ItemEvent.SELECTED)); 1285 } 1286 } 1287 1288 /** 1289 * Returns an array containing the selected item. 1290 * This method is implemented for compatibility with 1291 * <code>ItemSelectable</code>. 1292 * 1293 * @return an array of <code>Objects</code> containing one 1294 * element -- the selected item 1295 */ 1296 public Object[] getSelectedObjects() { 1297 Object selectedObject = getSelectedItem(); 1298 if ( selectedObject == null ) 1299 return new Object[0]; 1300 else { 1301 Object result[] = new Object[1]; 1302 result[0] = selectedObject; 1303 return result; 1304 } 1305 } 1306 1307 /** 1308 * This method is public as an implementation side effect. 1309 * do not call or override. 1310 */ 1311 public void actionPerformed(ActionEvent e) { 1312 setPopupVisible(false); 1313 getModel().setSelectedItem(getEditor().getItem()); 1314 String oldCommand = getActionCommand(); 1315 setActionCommand("comboBoxEdited"); 1316 fireActionEvent(); 1317 setActionCommand(oldCommand); 1318 } 1319 1320 /** 1321 * This method is public as an implementation side effect. 1322 * do not call or override. 1323 */ 1324 public void contentsChanged(ListDataEvent e) { 1325 Object oldSelection = selectedItemReminder; 1326 Object newSelection = dataModel.getSelectedItem(); 1327 if (oldSelection == null || !oldSelection.equals(newSelection)) { 1328 selectedItemChanged(); 1329 if (!selectingItem) { 1330 fireActionEvent(); 1331 } 1332 } 1333 } 1334 1335 /** 1336 * This method is public as an implementation side effect. 1337 * do not call or override. 1338 */ 1339 public void intervalAdded(ListDataEvent e) { 1340 if (selectedItemReminder != dataModel.getSelectedItem()) { 1341 selectedItemChanged(); 1342 } 1343 } 1344 1345 /** 1346 * This method is public as an implementation side effect. 1347 * do not call or override. 1348 */ 1349 public void intervalRemoved(ListDataEvent e) { 1350 contentsChanged(e); 1351 } 1352 1353 /** 1354 * Selects the list item that corresponds to the specified keyboard 1355 * character and returns true, if there is an item corresponding 1356 * to that character. Otherwise, returns false. 1357 * 1358 * @param keyChar a char, typically this is a keyboard key 1359 * typed by the user 1360 */ 1361 public boolean selectWithKeyChar(char keyChar) { 1362 int index; 1363 1364 if ( keySelectionManager == null ) 1365 keySelectionManager = createDefaultKeySelectionManager(); 1366 1367 index = keySelectionManager.selectionForKey(keyChar,getModel()); 1368 if ( index != -1 ) { 1369 setSelectedIndex(index); 1370 return true; 1371 } 1372 else 1373 return false; 1374 } 1375 1376 /** 1377 * Enables the combo box so that items can be selected. When the 1378 * combo box is disabled, items cannot be selected and values 1379 * cannot be typed into its field (if it is editable). 1380 * 1381 * @param b a boolean value, where true enables the component and 1382 * false disables it 1383 * @beaninfo 1384 * bound: true 1385 * preferred: true 1386 * description: Whether the combo box is enabled. 1387 */ 1388 public void setEnabled(boolean b) { 1389 super.setEnabled(b); 1390 firePropertyChange( "enabled", !isEnabled(), isEnabled() ); 1391 } 1392 1393 /** 1394 * Initializes the editor with the specified item. 1395 * 1396 * @param anEditor the <code>ComboBoxEditor</code> that displays 1397 * the list item in the 1398 * combo box field and allows it to be edited 1399 * @param anItem the object to display and edit in the field 1400 */ 1401 public void configureEditor(ComboBoxEditor anEditor, Object anItem) { 1402 anEditor.setItem(anItem); 1403 } 1404 1405 /** 1406 * Handles <code>KeyEvent</code>s, looking for the Tab key. 1407 * If the Tab key is found, the popup window is closed. 1408 * 1409 * @param e the <code>KeyEvent</code> containing the keyboard 1410 * key that was pressed 1411 */ 1412 public void processKeyEvent(KeyEvent e) { 1413 if ( e.getKeyCode() == KeyEvent.VK_TAB ) { 1414 hidePopup(); 1415 } 1416 super.processKeyEvent(e); 1417 } 1418 1419 /** 1420 * {@inheritDoc} 1421 */ 1422 @Override 1423 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { 1424 if (super.processKeyBinding(ks, e, condition, pressed)) { 1425 return true; 1426 } 1427 1428 if (!isEditable() || condition != WHEN_FOCUSED || getEditor() == null 1429 || !Boolean.TRUE.equals(getClientProperty("JComboBox.isTableCellEditor"))) { 1430 return false; 1431 } 1432 1433 Component editorComponent = getEditor().getEditorComponent(); 1434 if (editorComponent instanceof JComponent) { 1435 JComponent component = (JComponent) editorComponent; 1436 return component.processKeyBinding(ks, e, WHEN_FOCUSED, pressed); 1437 } 1438 return false; 1439 } 1440 1441 /** 1442 * Sets the object that translates a keyboard character into a list 1443 * selection. Typically, the first selection with a matching first 1444 * character becomes the selected item. 1445 * 1446 * @beaninfo 1447 * expert: true 1448 * description: The objects that changes the selection when a key is pressed. 1449 */ 1450 public void setKeySelectionManager(KeySelectionManager aManager) { 1451 keySelectionManager = aManager; 1452 } 1453 1454 /** 1455 * Returns the list's key-selection manager. 1456 * 1457 * @return the <code>KeySelectionManager</code> currently in use 1458 */ 1459 public KeySelectionManager getKeySelectionManager() { 1460 return keySelectionManager; 1461 } 1462 1463 /* Accessing the model */ 1464 /** 1465 * Returns the number of items in the list. 1466 * 1467 * @return an integer equal to the number of items in the list 1468 */ 1469 public int getItemCount() { 1470 return dataModel.getSize(); 1471 } 1472 1473 /** 1474 * Returns the list item at the specified index. If <code>index</code> 1475 * is out of range (less than zero or greater than or equal to size) 1476 * it will return <code>null</code>. 1477 * 1478 * @param index an integer indicating the list position, where the first 1479 * item starts at zero 1480 * @return the item at that list position; or 1481 * <code>null</code> if out of range 1482 */ 1483 public E getItemAt(int index) { 1484 return dataModel.getElementAt(index); 1485 } 1486 1487 /** 1488 * Returns an instance of the default key-selection manager. 1489 * 1490 * @return the <code>KeySelectionManager</code> currently used by the list 1491 * @see #setKeySelectionManager 1492 */ 1493 protected KeySelectionManager createDefaultKeySelectionManager() { 1494 return new DefaultKeySelectionManager(); 1495 } 1496 1497 1498 /** 1499 * The interface that defines a <code>KeySelectionManager</code>. 1500 * To qualify as a <code>KeySelectionManager</code>, 1501 * the class needs to implement the method 1502 * that identifies the list index given a character and the 1503 * combo box data model. 1504 */ 1505 public interface KeySelectionManager { 1506 /** Given <code>aKey</code> and the model, returns the row 1507 * that should become selected. Return -1 if no match was 1508 * found. 1509 * 1510 * @param aKey a char value, usually indicating a keyboard key that 1511 * was pressed 1512 * @param aModel a ComboBoxModel -- the component's data model, containing 1513 * the list of selectable items 1514 * @return an int equal to the selected row, where 0 is the 1515 * first item and -1 is none. 1516 */ 1517 int selectionForKey(char aKey,ComboBoxModel aModel); 1518 } 1519 1520 class DefaultKeySelectionManager implements KeySelectionManager, Serializable { 1521 public int selectionForKey(char aKey,ComboBoxModel aModel) { 1522 int i,c; 1523 int currentSelection = -1; 1524 Object selectedItem = aModel.getSelectedItem(); 1525 String v; 1526 String pattern; 1527 1528 if ( selectedItem != null ) { 1529 for ( i=0,c=aModel.getSize();i<c;i++ ) { 1530 if ( selectedItem == aModel.getElementAt(i) ) { 1531 currentSelection = i; 1532 break; 1533 } 1534 } 1535 } 1536 1537 pattern = ("" + aKey).toLowerCase(); 1538 aKey = pattern.charAt(0); 1539 1540 for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) { 1541 Object elem = aModel.getElementAt(i); 1542 if (elem != null && elem.toString() != null) { 1543 v = elem.toString().toLowerCase(); 1544 if ( v.length() > 0 && v.charAt(0) == aKey ) 1545 return i; 1546 } 1547 } 1548 1549 for ( i = 0 ; i < currentSelection ; i ++ ) { 1550 Object elem = aModel.getElementAt(i); 1551 if (elem != null && elem.toString() != null) { 1552 v = elem.toString().toLowerCase(); 1553 if ( v.length() > 0 && v.charAt(0) == aKey ) 1554 return i; 1555 } 1556 } 1557 return -1; 1558 } 1559 } 1560 1561 1562 /** 1563 * See <code>readObject</code> and <code>writeObject</code> in 1564 * <code>JComponent</code> for more 1565 * information about serialization in Swing. 1566 */ 1567 private void writeObject(ObjectOutputStream s) throws IOException { 1568 s.defaultWriteObject(); 1569 if (getUIClassID().equals(uiClassID)) { 1570 byte count = JComponent.getWriteObjCounter(this); 1571 JComponent.setWriteObjCounter(this, --count); 1572 if (count == 0 && ui != null) { 1573 ui.installUI(this); 1574 } 1575 } 1576 } 1577 1578 1579 /** 1580 * Returns a string representation of this <code>JComboBox</code>. 1581 * This method is intended to be used only for debugging purposes, 1582 * and the content and format of the returned string may vary between 1583 * implementations. The returned string may be empty but may not 1584 * be <code>null</code>. 1585 * 1586 * @return a string representation of this <code>JComboBox</code> 1587 */ 1588 protected String paramString() { 1589 String selectedItemReminderString = (selectedItemReminder != null ? 1590 selectedItemReminder.toString() : 1591 ""); 1592 String isEditableString = (isEditable ? "true" : "false"); 1593 String lightWeightPopupEnabledString = (lightWeightPopupEnabled ? 1594 "true" : "false"); 1595 1596 return super.paramString() + 1597 ",isEditable=" + isEditableString + 1598 ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString + 1599 ",maximumRowCount=" + maximumRowCount + 1600 ",selectedItemReminder=" + selectedItemReminderString; 1601 } 1602 1603 1604 /////////////////// 1605 // Accessibility support 1606 /////////////////// 1607 1608 /** 1609 * Gets the AccessibleContext associated with this JComboBox. 1610 * For combo boxes, the AccessibleContext takes the form of an 1611 * AccessibleJComboBox. 1612 * A new AccessibleJComboBox instance is created if necessary. 1613 * 1614 * @return an AccessibleJComboBox that serves as the 1615 * AccessibleContext of this JComboBox 1616 */ 1617 public AccessibleContext getAccessibleContext() { 1618 if ( accessibleContext == null ) { 1619 accessibleContext = new AccessibleJComboBox(); 1620 } 1621 return accessibleContext; 1622 } 1623 1624 /** 1625 * This class implements accessibility support for the 1626 * <code>JComboBox</code> class. It provides an implementation of the 1627 * Java Accessibility API appropriate to Combo Box user-interface elements. 1628 * <p> 1629 * <strong>Warning:</strong> 1630 * Serialized objects of this class will not be compatible with 1631 * future Swing releases. The current serialization support is 1632 * appropriate for short term storage or RMI between applications running 1633 * the same version of Swing. As of 1.4, support for long term storage 1634 * of all JavaBeans™ 1635 * has been added to the <code>java.beans</code> package. 1636 * Please see {@link java.beans.XMLEncoder}. 1637 */ 1638 protected class AccessibleJComboBox extends AccessibleJComponent 1639 implements AccessibleAction, AccessibleSelection { 1640 1641 1642 private JList popupList; // combo box popup list 1643 private Accessible previousSelectedAccessible = null; 1644 1645 /** 1646 * Returns an AccessibleJComboBox instance 1647 * @since 1.4 1648 */ 1649 public AccessibleJComboBox() { 1650 // set the combo box editor's accessible name and description 1651 JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener()); 1652 setEditorNameAndDescription(); 1653 1654 // Get the popup list 1655 Accessible a = getUI().getAccessibleChild(JComboBox.this, 0); 1656 if (a instanceof javax.swing.plaf.basic.ComboPopup) { 1657 // Listen for changes to the popup menu selection. 1658 popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList(); 1659 popupList.addListSelectionListener( 1660 new AccessibleJComboBoxListSelectionListener()); 1661 } 1662 // Listen for popup menu show/hide events 1663 JComboBox.this.addPopupMenuListener( 1664 new AccessibleJComboBoxPopupMenuListener()); 1665 } 1666 1667 /* 1668 * JComboBox PropertyChangeListener 1669 */ 1670 private class AccessibleJComboBoxPropertyChangeListener 1671 implements PropertyChangeListener { 1672 1673 public void propertyChange(PropertyChangeEvent e) { 1674 if (e.getPropertyName() == "editor") { 1675 // set the combo box editor's accessible name 1676 // and description 1677 setEditorNameAndDescription(); 1678 } 1679 } 1680 } 1681 1682 /* 1683 * Sets the combo box editor's accessible name and descripton 1684 */ 1685 private void setEditorNameAndDescription() { 1686 ComboBoxEditor editor = JComboBox.this.getEditor(); 1687 if (editor != null) { 1688 Component comp = editor.getEditorComponent(); 1689 if (comp instanceof Accessible) { 1690 AccessibleContext ac = comp.getAccessibleContext(); 1691 if (ac != null) { // may be null 1692 ac.setAccessibleName(getAccessibleName()); 1693 ac.setAccessibleDescription(getAccessibleDescription()); 1694 } 1695 } 1696 } 1697 } 1698 1699 /* 1700 * Listener for combo box popup menu 1701 * TIGER - 4669379 4894434 1702 */ 1703 private class AccessibleJComboBoxPopupMenuListener 1704 implements PopupMenuListener { 1705 1706 /** 1707 * This method is called before the popup menu becomes visible 1708 */ 1709 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 1710 // save the initial selection 1711 if (popupList == null) { 1712 return; 1713 } 1714 int selectedIndex = popupList.getSelectedIndex(); 1715 if (selectedIndex < 0) { 1716 return; 1717 } 1718 previousSelectedAccessible = 1719 popupList.getAccessibleContext().getAccessibleChild(selectedIndex); 1720 } 1721 1722 /** 1723 * This method is called before the popup menu becomes invisible 1724 * Note that a JPopupMenu can become invisible any time 1725 */ 1726 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 1727 // ignore 1728 } 1729 1730 /** 1731 * This method is called when the popup menu is canceled 1732 */ 1733 public void popupMenuCanceled(PopupMenuEvent e) { 1734 // ignore 1735 } 1736 } 1737 1738 /* 1739 * Handles changes to the popup list selection. 1740 * TIGER - 4669379 4894434 4933143 1741 */ 1742 private class AccessibleJComboBoxListSelectionListener 1743 implements ListSelectionListener { 1744 1745 public void valueChanged(ListSelectionEvent e) { 1746 if (popupList == null) { 1747 return; 1748 } 1749 1750 // Get the selected popup list item. 1751 int selectedIndex = popupList.getSelectedIndex(); 1752 if (selectedIndex < 0) { 1753 return; 1754 } 1755 Accessible selectedAccessible = 1756 popupList.getAccessibleContext().getAccessibleChild(selectedIndex); 1757 if (selectedAccessible == null) { 1758 return; 1759 } 1760 1761 // Fire a FOCUSED lost PropertyChangeEvent for the 1762 // previously selected list item. 1763 PropertyChangeEvent pce; 1764 1765 if (previousSelectedAccessible != null) { 1766 pce = new PropertyChangeEvent(previousSelectedAccessible, 1767 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1768 AccessibleState.FOCUSED, null); 1769 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1770 null, pce); 1771 } 1772 // Fire a FOCUSED gained PropertyChangeEvent for the 1773 // currently selected list item. 1774 pce = new PropertyChangeEvent(selectedAccessible, 1775 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1776 null, AccessibleState.FOCUSED); 1777 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1778 null, pce); 1779 1780 // Fire the ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY event 1781 // for the combo box. 1782 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, 1783 previousSelectedAccessible, selectedAccessible); 1784 1785 // Save the previous selection. 1786 previousSelectedAccessible = selectedAccessible; 1787 } 1788 } 1789 1790 1791 /** 1792 * Returns the number of accessible children in the object. If all 1793 * of the children of this object implement Accessible, than this 1794 * method should return the number of children of this object. 1795 * 1796 * @return the number of accessible children in the object. 1797 */ 1798 public int getAccessibleChildrenCount() { 1799 // Always delegate to the UI if it exists 1800 if (ui != null) { 1801 return ui.getAccessibleChildrenCount(JComboBox.this); 1802 } else { 1803 return super.getAccessibleChildrenCount(); 1804 } 1805 } 1806 1807 /** 1808 * Returns the nth Accessible child of the object. 1809 * The child at index zero represents the popup. 1810 * If the combo box is editable, the child at index one 1811 * represents the editor. 1812 * 1813 * @param i zero-based index of child 1814 * @return the nth Accessible child of the object 1815 */ 1816 public Accessible getAccessibleChild(int i) { 1817 // Always delegate to the UI if it exists 1818 if (ui != null) { 1819 return ui.getAccessibleChild(JComboBox.this, i); 1820 } else { 1821 return super.getAccessibleChild(i); 1822 } 1823 } 1824 1825 /** 1826 * Get the role of this object. 1827 * 1828 * @return an instance of AccessibleRole describing the role of the 1829 * object 1830 * @see AccessibleRole 1831 */ 1832 public AccessibleRole getAccessibleRole() { 1833 return AccessibleRole.COMBO_BOX; 1834 } 1835 1836 /** 1837 * Gets the state set of this object. The AccessibleStateSet of 1838 * an object is composed of a set of unique AccessibleStates. 1839 * A change in the AccessibleStateSet of an object will cause a 1840 * PropertyChangeEvent to be fired for the ACCESSIBLE_STATE_PROPERTY 1841 * property. 1842 * 1843 * @return an instance of AccessibleStateSet containing the 1844 * current state set of the object 1845 * @see AccessibleStateSet 1846 * @see AccessibleState 1847 * @see #addPropertyChangeListener 1848 * 1849 */ 1850 public AccessibleStateSet getAccessibleStateSet() { 1851 // TIGER - 4489748 1852 AccessibleStateSet ass = super.getAccessibleStateSet(); 1853 if (ass == null) { 1854 ass = new AccessibleStateSet(); 1855 } 1856 if (JComboBox.this.isPopupVisible()) { 1857 ass.add(AccessibleState.EXPANDED); 1858 } else { 1859 ass.add(AccessibleState.COLLAPSED); 1860 } 1861 return ass; 1862 } 1863 1864 /** 1865 * Get the AccessibleAction associated with this object. In the 1866 * implementation of the Java Accessibility API for this class, 1867 * return this object, which is responsible for implementing the 1868 * AccessibleAction interface on behalf of itself. 1869 * 1870 * @return this object 1871 */ 1872 public AccessibleAction getAccessibleAction() { 1873 return this; 1874 } 1875 1876 /** 1877 * Return a description of the specified action of the object. 1878 * 1879 * @param i zero-based index of the actions 1880 */ 1881 public String getAccessibleActionDescription(int i) { 1882 if (i == 0) { 1883 return UIManager.getString("ComboBox.togglePopupText"); 1884 } 1885 else { 1886 return null; 1887 } 1888 } 1889 1890 /** 1891 * Returns the number of Actions available in this object. The 1892 * default behavior of a combo box is to have one action. 1893 * 1894 * @return 1, the number of Actions in this object 1895 */ 1896 public int getAccessibleActionCount() { 1897 return 1; 1898 } 1899 1900 /** 1901 * Perform the specified Action on the object 1902 * 1903 * @param i zero-based index of actions 1904 * @return true if the the action was performed; else false. 1905 */ 1906 public boolean doAccessibleAction(int i) { 1907 if (i == 0) { 1908 setPopupVisible(!isPopupVisible()); 1909 return true; 1910 } 1911 else { 1912 return false; 1913 } 1914 } 1915 1916 1917 /** 1918 * Get the AccessibleSelection associated with this object. In the 1919 * implementation of the Java Accessibility API for this class, 1920 * return this object, which is responsible for implementing the 1921 * AccessibleSelection interface on behalf of itself. 1922 * 1923 * @return this object 1924 */ 1925 public AccessibleSelection getAccessibleSelection() { 1926 return this; 1927 } 1928 1929 /** 1930 * Returns the number of Accessible children currently selected. 1931 * If no children are selected, the return value will be 0. 1932 * 1933 * @return the number of items currently selected. 1934 * @since 1.3 1935 */ 1936 public int getAccessibleSelectionCount() { 1937 Object o = JComboBox.this.getSelectedItem(); 1938 if (o != null) { 1939 return 1; 1940 } else { 1941 return 0; 1942 } 1943 } 1944 1945 /** 1946 * Returns an Accessible representing the specified selected child 1947 * in the popup. If there isn't a selection, or there are 1948 * fewer children selected than the integer passed in, the return 1949 * value will be null. 1950 * <p>Note that the index represents the i-th selected child, which 1951 * is different from the i-th child. 1952 * 1953 * @param i the zero-based index of selected children 1954 * @return the i-th selected child 1955 * @see #getAccessibleSelectionCount 1956 * @since 1.3 1957 */ 1958 public Accessible getAccessibleSelection(int i) { 1959 // Get the popup 1960 Accessible a = 1961 JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0); 1962 if (a != null && 1963 a instanceof javax.swing.plaf.basic.ComboPopup) { 1964 1965 // get the popup list 1966 JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList(); 1967 1968 // return the i-th selection in the popup list 1969 AccessibleContext ac = list.getAccessibleContext(); 1970 if (ac != null) { 1971 AccessibleSelection as = ac.getAccessibleSelection(); 1972 if (as != null) { 1973 return as.getAccessibleSelection(i); 1974 } 1975 } 1976 } 1977 return null; 1978 } 1979 1980 /** 1981 * Determines if the current child of this object is selected. 1982 * 1983 * @return true if the current child of this object is selected; 1984 * else false 1985 * @param i the zero-based index of the child in this Accessible 1986 * object. 1987 * @see AccessibleContext#getAccessibleChild 1988 * @since 1.3 1989 */ 1990 public boolean isAccessibleChildSelected(int i) { 1991 return JComboBox.this.getSelectedIndex() == i; 1992 } 1993 1994 /** 1995 * Adds the specified Accessible child of the object to the object's 1996 * selection. If the object supports multiple selections, 1997 * the specified child is added to any existing selection, otherwise 1998 * it replaces any existing selection in the object. If the 1999 * specified child is already selected, this method has no effect. 2000 * 2001 * @param i the zero-based index of the child 2002 * @see AccessibleContext#getAccessibleChild 2003 * @since 1.3 2004 */ 2005 public void addAccessibleSelection(int i) { 2006 // TIGER - 4856195 2007 clearAccessibleSelection(); 2008 JComboBox.this.setSelectedIndex(i); 2009 } 2010 2011 /** 2012 * Removes the specified child of the object from the object's 2013 * selection. If the specified item isn't currently selected, this 2014 * method has no effect. 2015 * 2016 * @param i the zero-based index of the child 2017 * @see AccessibleContext#getAccessibleChild 2018 * @since 1.3 2019 */ 2020 public void removeAccessibleSelection(int i) { 2021 if (JComboBox.this.getSelectedIndex() == i) { 2022 clearAccessibleSelection(); 2023 } 2024 } 2025 2026 /** 2027 * Clears the selection in the object, so that no children in the 2028 * object are selected. 2029 * @since 1.3 2030 */ 2031 public void clearAccessibleSelection() { 2032 JComboBox.this.setSelectedIndex(-1); 2033 } 2034 2035 /** 2036 * Causes every child of the object to be selected 2037 * if the object supports multiple selections. 2038 * @since 1.3 2039 */ 2040 public void selectAllAccessibleSelection() { 2041 // do nothing since multiple selection is not supported 2042 } 2043 2044 // public Accessible getAccessibleAt(Point p) { 2045 // Accessible a = getAccessibleChild(1); 2046 // if ( a != null ) { 2047 // return a; // the editor 2048 // } 2049 // else { 2050 // return getAccessibleChild(0); // the list 2051 // } 2052 // } 2053 private EditorAccessibleContext editorAccessibleContext = null; 2054 2055 private class AccessibleEditor implements Accessible { 2056 public AccessibleContext getAccessibleContext() { 2057 if (editorAccessibleContext == null) { 2058 Component c = JComboBox.this.getEditor().getEditorComponent(); 2059 if (c instanceof Accessible) { 2060 editorAccessibleContext = 2061 new EditorAccessibleContext((Accessible)c); 2062 } 2063 } 2064 return editorAccessibleContext; 2065 } 2066 } 2067 2068 /* 2069 * Wrapper class for the AccessibleContext implemented by the 2070 * combo box editor. Delegates all method calls except 2071 * getAccessibleIndexInParent to the editor. The 2072 * getAccessibleIndexInParent method returns the selected 2073 * index in the combo box. 2074 */ 2075 private class EditorAccessibleContext extends AccessibleContext { 2076 2077 private AccessibleContext ac; 2078 2079 private EditorAccessibleContext() { 2080 } 2081 2082 /* 2083 * @param a the AccessibleContext implemented by the 2084 * combo box editor 2085 */ 2086 EditorAccessibleContext(Accessible a) { 2087 this.ac = a.getAccessibleContext(); 2088 } 2089 2090 /** 2091 * Gets the accessibleName property of this object. The accessibleName 2092 * property of an object is a localized String that designates the purpose 2093 * of the object. For example, the accessibleName property of a label 2094 * or button might be the text of the label or button itself. In the 2095 * case of an object that doesn't display its name, the accessibleName 2096 * should still be set. For example, in the case of a text field used 2097 * to enter the name of a city, the accessibleName for the en_US locale 2098 * could be 'city.' 2099 * 2100 * @return the localized name of the object; null if this 2101 * object does not have a name 2102 * 2103 * @see #setAccessibleName 2104 */ 2105 public String getAccessibleName() { 2106 return ac.getAccessibleName(); 2107 } 2108 2109 /** 2110 * Sets the localized accessible name of this object. Changing the 2111 * name will cause a PropertyChangeEvent to be fired for the 2112 * ACCESSIBLE_NAME_PROPERTY property. 2113 * 2114 * @param s the new localized name of the object. 2115 * 2116 * @see #getAccessibleName 2117 * @see #addPropertyChangeListener 2118 * 2119 * @beaninfo 2120 * preferred: true 2121 * description: Sets the accessible name for the component. 2122 */ 2123 public void setAccessibleName(String s) { 2124 ac.setAccessibleName(s); 2125 } 2126 2127 /** 2128 * Gets the accessibleDescription property of this object. The 2129 * accessibleDescription property of this object is a short localized 2130 * phrase describing the purpose of the object. For example, in the 2131 * case of a 'Cancel' button, the accessibleDescription could be 2132 * 'Ignore changes and close dialog box.' 2133 * 2134 * @return the localized description of the object; null if 2135 * this object does not have a description 2136 * 2137 * @see #setAccessibleDescription 2138 */ 2139 public String getAccessibleDescription() { 2140 return ac.getAccessibleDescription(); 2141 } 2142 2143 /** 2144 * Sets the accessible description of this object. Changing the 2145 * name will cause a PropertyChangeEvent to be fired for the 2146 * ACCESSIBLE_DESCRIPTION_PROPERTY property. 2147 * 2148 * @param s the new localized description of the object 2149 * 2150 * @see #setAccessibleName 2151 * @see #addPropertyChangeListener 2152 * 2153 * @beaninfo 2154 * preferred: true 2155 * description: Sets the accessible description for the component. 2156 */ 2157 public void setAccessibleDescription(String s) { 2158 ac.setAccessibleDescription(s); 2159 } 2160 2161 /** 2162 * Gets the role of this object. The role of the object is the generic 2163 * purpose or use of the class of this object. For example, the role 2164 * of a push button is AccessibleRole.PUSH_BUTTON. The roles in 2165 * AccessibleRole are provided so component developers can pick from 2166 * a set of predefined roles. This enables assistive technologies to 2167 * provide a consistent interface to various tweaked subclasses of 2168 * components (e.g., use AccessibleRole.PUSH_BUTTON for all components 2169 * that act like a push button) as well as distinguish between subclasses 2170 * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes 2171 * and AccessibleRole.RADIO_BUTTON for radio buttons). 2172 * <p>Note that the AccessibleRole class is also extensible, so 2173 * custom component developers can define their own AccessibleRole's 2174 * if the set of predefined roles is inadequate. 2175 * 2176 * @return an instance of AccessibleRole describing the role of the object 2177 * @see AccessibleRole 2178 */ 2179 public AccessibleRole getAccessibleRole() { 2180 return ac.getAccessibleRole(); 2181 } 2182 2183 /** 2184 * Gets the state set of this object. The AccessibleStateSet of an object 2185 * is composed of a set of unique AccessibleStates. A change in the 2186 * AccessibleStateSet of an object will cause a PropertyChangeEvent to 2187 * be fired for the ACCESSIBLE_STATE_PROPERTY property. 2188 * 2189 * @return an instance of AccessibleStateSet containing the 2190 * current state set of the object 2191 * @see AccessibleStateSet 2192 * @see AccessibleState 2193 * @see #addPropertyChangeListener 2194 */ 2195 public AccessibleStateSet getAccessibleStateSet() { 2196 return ac.getAccessibleStateSet(); 2197 } 2198 2199 /** 2200 * Gets the Accessible parent of this object. 2201 * 2202 * @return the Accessible parent of this object; null if this 2203 * object does not have an Accessible parent 2204 */ 2205 public Accessible getAccessibleParent() { 2206 return ac.getAccessibleParent(); 2207 } 2208 2209 /** 2210 * Sets the Accessible parent of this object. This is meant to be used 2211 * only in the situations where the actual component's parent should 2212 * not be treated as the component's accessible parent and is a method 2213 * that should only be called by the parent of the accessible child. 2214 * 2215 * @param a - Accessible to be set as the parent 2216 */ 2217 public void setAccessibleParent(Accessible a) { 2218 ac.setAccessibleParent(a); 2219 } 2220 2221 /** 2222 * Gets the 0-based index of this object in its accessible parent. 2223 * 2224 * @return the 0-based index of this object in its parent; -1 if this 2225 * object does not have an accessible parent. 2226 * 2227 * @see #getAccessibleParent 2228 * @see #getAccessibleChildrenCount 2229 * @see #getAccessibleChild 2230 */ 2231 public int getAccessibleIndexInParent() { 2232 return JComboBox.this.getSelectedIndex(); 2233 } 2234 2235 /** 2236 * Returns the number of accessible children of the object. 2237 * 2238 * @return the number of accessible children of the object. 2239 */ 2240 public int getAccessibleChildrenCount() { 2241 return ac.getAccessibleChildrenCount(); 2242 } 2243 2244 /** 2245 * Returns the specified Accessible child of the object. The Accessible 2246 * children of an Accessible object are zero-based, so the first child 2247 * of an Accessible child is at index 0, the second child is at index 1, 2248 * and so on. 2249 * 2250 * @param i zero-based index of child 2251 * @return the Accessible child of the object 2252 * @see #getAccessibleChildrenCount 2253 */ 2254 public Accessible getAccessibleChild(int i) { 2255 return ac.getAccessibleChild(i); 2256 } 2257 2258 /** 2259 * Gets the locale of the component. If the component does not have a 2260 * locale, then the locale of its parent is returned. 2261 * 2262 * @return this component's locale. If this component does not have 2263 * a locale, the locale of its parent is returned. 2264 * 2265 * @exception IllegalComponentStateException 2266 * If the Component does not have its own locale and has not yet been 2267 * added to a containment hierarchy such that the locale can be 2268 * determined from the containing parent. 2269 */ 2270 public Locale getLocale() throws IllegalComponentStateException { 2271 return ac.getLocale(); 2272 } 2273 2274 /** 2275 * Adds a PropertyChangeListener to the listener list. 2276 * The listener is registered for all Accessible properties and will 2277 * be called when those properties change. 2278 * 2279 * @see #ACCESSIBLE_NAME_PROPERTY 2280 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY 2281 * @see #ACCESSIBLE_STATE_PROPERTY 2282 * @see #ACCESSIBLE_VALUE_PROPERTY 2283 * @see #ACCESSIBLE_SELECTION_PROPERTY 2284 * @see #ACCESSIBLE_TEXT_PROPERTY 2285 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY 2286 * 2287 * @param listener The PropertyChangeListener to be added 2288 */ 2289 public void addPropertyChangeListener(PropertyChangeListener listener) { 2290 ac.addPropertyChangeListener(listener); 2291 } 2292 2293 /** 2294 * Removes a PropertyChangeListener from the listener list. 2295 * This removes a PropertyChangeListener that was registered 2296 * for all properties. 2297 * 2298 * @param listener The PropertyChangeListener to be removed 2299 */ 2300 public void removePropertyChangeListener(PropertyChangeListener listener) { 2301 ac.removePropertyChangeListener(listener); 2302 } 2303 2304 /** 2305 * Gets the AccessibleAction associated with this object that supports 2306 * one or more actions. 2307 * 2308 * @return AccessibleAction if supported by object; else return null 2309 * @see AccessibleAction 2310 */ 2311 public AccessibleAction getAccessibleAction() { 2312 return ac.getAccessibleAction(); 2313 } 2314 2315 /** 2316 * Gets the AccessibleComponent associated with this object that has a 2317 * graphical representation. 2318 * 2319 * @return AccessibleComponent if supported by object; else return null 2320 * @see AccessibleComponent 2321 */ 2322 public AccessibleComponent getAccessibleComponent() { 2323 return ac.getAccessibleComponent(); 2324 } 2325 2326 /** 2327 * Gets the AccessibleSelection associated with this object which allows its 2328 * Accessible children to be selected. 2329 * 2330 * @return AccessibleSelection if supported by object; else return null 2331 * @see AccessibleSelection 2332 */ 2333 public AccessibleSelection getAccessibleSelection() { 2334 return ac.getAccessibleSelection(); 2335 } 2336 2337 /** 2338 * Gets the AccessibleText associated with this object presenting 2339 * text on the display. 2340 * 2341 * @return AccessibleText if supported by object; else return null 2342 * @see AccessibleText 2343 */ 2344 public AccessibleText getAccessibleText() { 2345 return ac.getAccessibleText(); 2346 } 2347 2348 /** 2349 * Gets the AccessibleEditableText associated with this object 2350 * presenting editable text on the display. 2351 * 2352 * @return AccessibleEditableText if supported by object; else return null 2353 * @see AccessibleEditableText 2354 */ 2355 public AccessibleEditableText getAccessibleEditableText() { 2356 return ac.getAccessibleEditableText(); 2357 } 2358 2359 /** 2360 * Gets the AccessibleValue associated with this object that supports a 2361 * Numerical value. 2362 * 2363 * @return AccessibleValue if supported by object; else return null 2364 * @see AccessibleValue 2365 */ 2366 public AccessibleValue getAccessibleValue() { 2367 return ac.getAccessibleValue(); 2368 } 2369 2370 /** 2371 * Gets the AccessibleIcons associated with an object that has 2372 * one or more associated icons 2373 * 2374 * @return an array of AccessibleIcon if supported by object; 2375 * otherwise return null 2376 * @see AccessibleIcon 2377 */ 2378 public AccessibleIcon [] getAccessibleIcon() { 2379 return ac.getAccessibleIcon(); 2380 } 2381 2382 /** 2383 * Gets the AccessibleRelationSet associated with an object 2384 * 2385 * @return an AccessibleRelationSet if supported by object; 2386 * otherwise return null 2387 * @see AccessibleRelationSet 2388 */ 2389 public AccessibleRelationSet getAccessibleRelationSet() { 2390 return ac.getAccessibleRelationSet(); 2391 } 2392 2393 /** 2394 * Gets the AccessibleTable associated with an object 2395 * 2396 * @return an AccessibleTable if supported by object; 2397 * otherwise return null 2398 * @see AccessibleTable 2399 */ 2400 public AccessibleTable getAccessibleTable() { 2401 return ac.getAccessibleTable(); 2402 } 2403 2404 /** 2405 * Support for reporting bound property changes. If oldValue and 2406 * newValue are not equal and the PropertyChangeEvent listener list 2407 * is not empty, then fire a PropertyChange event to each listener. 2408 * In general, this is for use by the Accessible objects themselves 2409 * and should not be called by an application program. 2410 * @param propertyName The programmatic name of the property that 2411 * was changed. 2412 * @param oldValue The old value of the property. 2413 * @param newValue The new value of the property. 2414 * @see java.beans.PropertyChangeSupport 2415 * @see #addPropertyChangeListener 2416 * @see #removePropertyChangeListener 2417 * @see #ACCESSIBLE_NAME_PROPERTY 2418 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY 2419 * @see #ACCESSIBLE_STATE_PROPERTY 2420 * @see #ACCESSIBLE_VALUE_PROPERTY 2421 * @see #ACCESSIBLE_SELECTION_PROPERTY 2422 * @see #ACCESSIBLE_TEXT_PROPERTY 2423 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY 2424 */ 2425 public void firePropertyChange(String propertyName, 2426 Object oldValue, 2427 Object newValue) { 2428 ac.firePropertyChange(propertyName, oldValue, newValue); 2429 } 2430 } 2431 2432 } // innerclass AccessibleJComboBox 2433 }