-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
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;
}
}
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;
}
}