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

JComboBox getSelectedIndex method will not work for duplicate entries

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.3.0
    • 1.2.0
    • docs
    • kestrel
    • generic
    • generic



      Name: tb29552 Date: 11/13/98


      /*
      This doc bug is a follow-on to Bug ID 4133743
       Synopsis: "JComboBox.getSelectedIndex fails with duplicate entries"

      Perhaps it ought to be taken into consideration to
      change the documentation for JComboBox to reflect this
      behavior, because although this may not be a "bug" the
      documentation makes no mention that this is how the
      combo box functions.

      The documentation states:
      "Returns the index of the currently selected item in the list."

      which it clearly does not do in the case where the same
      item is inserted into the list several times.

      Perhaps more accurate would be:
      "The first item in the list that matches the given item will be returned."

                      JComboBox.getSelectedIndex() bug

                      Reported by: Michael Kovacs
                      ###@###.###
      */

      import com.sun.java.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      public class JFrame1 extends JFrame
       implements ActionListener {
          JComboBox jComboBox1 = new JComboBox();
          JTextField jTextField1 = new JTextField();

          public JFrame1 () {
              String comboBox[] = {"selection 1",
                                   "selection 2",
                                   "selection 2",
                                   "selection 3"};

              setVisible(false);
              setSize(405, 305);
              JPanel panel = new JPanel();

              jComboBox1.setPreferredSize(new Dimension(120, 20));
              jTextField1.setPreferredSize(new Dimension(120, 20));

              panel.add(jComboBox1);
              panel.add(jTextField1);
              getContentPane().add(panel);

              for (int x = 0; x < 4; x++) {
                  jComboBox1.addItem(comboBox[x]);
              }
              jComboBox1.addActionListener(this);
          }

          public void actionPerformed(ActionEvent event) {
              if (event.getSource() == jComboBox1) {
                  jTextField1.setText("selectedindex: " +
                                      jComboBox1.getSelectedIndex());
                  // the getSelectedIndex is reporting the same index value for two
                  // different combobox items that have the same name
              }
          }

          static public void main(String args[]) {
              (new JFrame1 ()).setVisible(true);
          }
      }

      (Review ID: 41448)
      ======================================================================

            sharonz Sharon Zakhour (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: