Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4909090

selectionForKey in JComboBox using different way for finding selected index

XMLWordPrintable

      Name: jk109818 Date: 08/19/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.1_03-b02, mixed mode)

      FULL OS VERSION :
      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      There are two different ways to find the selected index in a JComboBox, which can be a problem when using your own ComboBoxModel. The first one (which I think is correct) is in getSelectedIndex and is using equals:

          public int getSelectedIndex() {
              Object sObject = dataModel.getSelectedItem();
              int i,c;
              Object obj;

              for ( i=0,c=dataModel.getSize();i<c;i++ ) {
                  obj = dataModel.getElementAt(i);
                  if ( obj != null && obj.equals(sObject) )
                      return i;
              }
              return -1;
          }

      The second version is in the inner class DefaultKeySelectionManager and is using '=='. This implies that the model is using the same object in the list and for the selected item (which isn't necessary in the first case):

              public int selectionForKey(char aKey,ComboBoxModel aModel) {
                  int i,c;
                  int currentSelection = -1;
                  Object selectedItem = aModel.getSelectedItem();
                  String v;
                  String pattern;

                  if ( selectedItem != null ) {
                      for ( i=0,c=aModel.getSize();i<c;i++ ) {
                          if ( selectedItem == aModel.getElementAt(i) ) {
                              currentSelection = i;
                              break;
                          }
                      }
                  }
                  ...

      When using a ComboBoxModel with different objects for the list-entries and the selected item, this version is unable to find the correct index, so the first-letter-navigation will always find the first entry in the list that starts with the entered character.

      There should be only one way to find the selected index (I prefer the first one)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Change the model so getSelectedItem returns the object from the list.
      (Incident Review ID: 191116)
      ======================================================================

            Unassigned Unassigned
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: