import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
public class Main {
public static void 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;
public void 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
} else if ("comboBoxChanged".equals(evt.getActionCommand())) {
// User has selected an item; it may be the same item
}
}
}