import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
publicclass Main {
publicstaticvoid main(String[] argv) throws Exception {
String[] items = { "item1", "item2" };
JComboBox cb = new JComboBox(items);
cb.setEditable(true);
// Create and register listener
MyActionListener actionListener = new MyActionListener();
cb.addActionListener(actionListener);
}
}
class MyActionListener implements ActionListener {
Object oldItem;
publicvoid actionPerformed(ActionEvent evt) {
JComboBox cb = (JComboBox) evt.getSource();
Object newItem = cb.getSelectedItem();
boolean same = newItem.equals(oldItem);
oldItem = newItem;
if ("comboBoxEdited".equals(evt.getActionCommand())) {
// User has typed in a string; only possible with an editable combobox
} elseif ("comboBoxChanged".equals(evt.getActionCommand())) {
// User has selected an item; it may be the same item
}
}
}