< prev index next >

src/share/classes/javax/swing/JComboBox.java

Print this page
rev 14331 : 8072767: DefaultCellEditor for comboBox creates ActionEvent with wrong source object
Reviewed-by: serb, azvegint


 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 
 573             // Must toggle the state of this flag since this method
 574             // call may result in ListDataEvents being fired.
 575             selectingItem = true;
 576             dataModel.setSelectedItem(objectToSelect);
 577             selectingItem = false;
 578 
 579             if (selectedItemReminder != dataModel.getSelectedItem()) {
 580                 // in case a users implementation of ComboBoxModel
 581                 // doesn't fire a ListDataEvent when the selection
 582                 // changes.
 583                 selectedItemChanged();
 584             }
 585         }
 586         fireActionEvent();
 587     }
 588 
 589     /**
 590      * Returns the current selected item.


1290      *
1291      * @return an array of <code>Objects</code> containing one
1292      *          element -- the selected item
1293      */
1294     public Object[] getSelectedObjects() {
1295         Object selectedObject = getSelectedItem();
1296         if ( selectedObject == null )
1297             return new Object[0];
1298         else {
1299             Object result[] = new Object[1];
1300             result[0] = selectedObject;
1301             return result;
1302         }
1303     }
1304 
1305     /**
1306      * This method is public as an implementation side effect.
1307      * do not call or override.
1308      */
1309     public void actionPerformed(ActionEvent e) {
1310         ComboBoxEditor editor = getEditor();
1311         if ((editor != null) && (e != null) && (editor == e.getSource()
1312                 || editor.getEditorComponent() == e.getSource())) {
1313             setPopupVisible(false);
1314             getModel().setSelectedItem(editor.getItem());
1315             String oldCommand = getActionCommand();
1316             setActionCommand("comboBoxEdited");
1317             fireActionEvent();
1318             setActionCommand(oldCommand);
1319         }
1320     }
1321 
1322     /**
1323      * This method is public as an implementation side effect.
1324      * do not call or override.
1325      */
1326     public void contentsChanged(ListDataEvent e) {
1327         Object oldSelection = selectedItemReminder;
1328         Object newSelection = dataModel.getSelectedItem();
1329         if (oldSelection == null || !oldSelection.equals(newSelection)) {
1330             selectedItemChanged();
1331             if (!selectingItem) {
1332                 fireActionEvent();
1333             }
1334         }
1335     }
1336 
1337     /**
1338      * This method is public as an implementation side effect.
1339      * do not call or override.




 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.


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.


< prev index next >