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

JComboBox fires extra event without (or as it loses) focus

XMLWordPrintable

    • merlin
    • generic, x86
    • generic, windows_nt

         Customer is running Java apps on Windows95/98/NT on Pentium systems. They
      found that changing the state of an editable JComboBox field without hitting enter then giving focus to another component such as button and clicking on it will cause a ButtonEvent but also a DeselectEvent and SelectEvent on the JComboBox. The expected behavior is only a ButtonEvent being fired.

      The testcase is below:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.border.*;

      class ComboBoxPanel extends JPanel implements ItemListener {
          ComboBoxPanel() {
              String[] items = {"item1", "item2", "item3"};
              JComboBox editableComboBox = new JComboBox(items);
              editableComboBox.setEditable(true);
              editableComboBox.addItemListener(this);
              TitledBorder titledBorder = new TitledBorder("My ComboBox:");
              titledBorder.setTitlePosition(TitledBorder.TOP);
              titledBorder.setTitleJustification(TitledBorder.LEFT);
              add(editableComboBox);
              setBorder(titledBorder);
              setMaximumSize(getPreferredSize());
          }
          
          public void itemStateChanged(ItemEvent e) {
              System.out.println(" ComboBox " + (e.getStateChange()==ItemEvent.SELECTED? "SELECTED" : "DESELECTED") + "\n");
          }
      }

      class ButtonPanel extends JPanel implements ActionListener {
          ButtonPanel() {
              setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
              JButton btn1 = new JButton("Button1");
              btn1.addActionListener(this);
              add(btn1);
              JButton btn2 = new JButton("Button2");
              btn2.addActionListener(this);
              add(btn2);
              setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(2,2,2,2)));
          }

          public void actionPerformed(ActionEvent e) {
              System.out.println(" ButtonEvent\n");
          }
      }

      public class Test {
          public static void main(String[] args) {
              JFrame frame = new JFrame("Test");
              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
              frame.getContentPane().add(new ComboBoxPanel(), BorderLayout.CENTER);
              frame.getContentPane().add(new ButtonPanel(), BorderLayout.SOUTH);
              frame.pack();
              frame.setResizable(true);
              frame.setVisible(true);
          }
      }



      Name: krT82822 Date: 08/05/99


      8/4/99 kevin.ryan@eng -- as user notes, it's related to bug # 4239610, but also to bug # 4199694. Unfortunately, the latter bug is already closed,
      so duping this to # 4239610 seems reasonable. Will alter that bug's synopsis to help incorporate this new info.

      orig synopsis: "An editable JComboBox with non-String items fires a bad itemStateChanged"

      Create an editable JComboBox, and add to it some items that aren't Strings (e.g. Integers). When you choose an item from the list using the mouse, an itemStateChanged correctly fires, and if you call getSelectedItem() on the JComboBox, it returns the correct object (an Integer).

      But if you then tab out of the JComboBox, another itemStateChanged fires, even though you haven't changed the value. Now if you call getSelectedItem(), the object is a String instead of an Integer. When using more complex types than Integer, this can cause a loss of significant information - the selected item is returned as a String, and all the information in the underlying object is lost. (Note that if you load up the JComboBox with Strings, the second itemStateChanged doesn't fire, and everything works fine.)

      This bug is similar to, but different from, bug 4239610 (the sample code there fills the JComboBox with Strings instead of other objects).


      import java.util.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class ComboTest implements ItemListener
      {
      public final static void main (String[] args)
      {
      ComboTest test = new ComboTest();
      }

      private ComboTest()
      {
      // Create a frame
      JFrame f = new JFrame("hi");
      f.getContentPane().setLayout(null);
      f.setBounds(100,100,400,200);

      // Add a combobox
      JComboBox t = new JComboBox();
      t.setBounds(50,50,80,25);
      t.setEditable(true);
      t.addItem(new Integer(1));
      t.addItem(new Integer(2));
      t.addItem(new Integer(3));
      t.addItemListener(this);
      f.getContentPane().add(t);

      // Add a text field (so we can tab out of the combobox)
      JTextField tf = new JTextField();
      tf.setBounds(50,100,80,25);
      f.getContentPane().add(tf);

      // Show it
      f.show();
      }

      // When we get an itemStateChanged event, we print out the currently selected item.
      public void itemStateChanged(ItemEvent e)
      {
      // itemStateChanged fires twice, once for the item that gets deselected, and then
      // once for the item that gets selected. We only want to print out onces, to make
      // things less confusing.
      if (e.getStateChange() == ItemEvent.SELECTED)
      {
      JComboBox t = (JComboBox)e.getSource();
      Object obj = t.getSelectedItem();

      System.out.println(obj + " " + obj.getClass().getName());
      }
      }
      }

      This is the output, when you choose item 2 from the dropdown, and then tab out of the control:

      2 java.lang.Integer
      2 java.lang.String
      (Review ID: 93533)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            atongschsunw Albert Tong-schmidt (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: