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

under jdk1.4, invoking ComboBoxModel.setSelectedItem() does not create ItemEvent

XMLWordPrintable

    • beta2
    • sparc
    • solaris_8

      Compile and run the following codes, you can find that under jdk1.2 ComboBoxModel.setSelectedItem() creates ItemEvent, but under jdk1.4, no ItemEvent is created.

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

      public class Test {
          public Test() {
              JFrame f = new JFrame("Test ComboBox");
      JComboBox combo = new JComboBox();
      combo.setModel(new MyModel());
      combo.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent evt) {
      System.out.println("selection changed: " + evt.getItem());
      }
      });

      f.getContentPane().setLayout(new FlowLayout());
      f.getContentPane().add(combo);
      ((MyModel)combo.getModel()).setSelectedItem("bac");
      f.setSize(200, 200);
      f.setVisible(true);
          }
          
          public static void main(String[] args) {
              new Test();
          }
      }

      class MyModel extends AbstractListModel implements ComboBoxModel {
          String[] items = {"abc", "bac", "cba"};
          Object currentValue;

          public void setSelectedItem(Object anObject) {
              currentValue = anObject;
              fireContentsChanged(this, -1, -1);
          }

          public Object getSelectedItem() {
              return currentValue;
          }

          public Object getElementAt(int index) {
              return items[index];
          }

          public int getSize() {
              return items.length;
          }
      }

            mdavidsosunw Mark Davidson (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: