Reputation: 459
How can I determine whether an ActionEvent fired by a JComboBox is caused by the user from the GUI, or is caused by calling comboBox.setSelectedItem("something")
from code? Or from any other event?
Upvotes: 2
Views: 298
Reputation: 50041
Use a boolean variable to keep track of when you are changing the value yourself:
private JComboBox<String> comboBox;
private boolean comboBoxChangedFromCode = false;
Set that variable while you change the value:
comboBoxChangedFromCode = true;
comboBox.setSelectedItem(...);
comboBoxChangedFromCode = false;
Check the value in your combo box's ActionListener:
public void actionPerformed(ActionEvent e) {
if (comboBoxChangedFromCode) {
...
} else {
...
}
}
Upvotes: 1