Question

I'd like to update GUI elements (JComboBox, JLabel, etc.) from code which shouldn't trigger change event. Is it possible to find out from java.awt.event.ActionEvent or java.awt.event.ItemEvent if the change was caused by an user or by running code like this?

combo.setSelectedItem("my item")
Était-ce utile?

La solution

The answer is: no.

But in some cases you can try to analyze the current InputEvent. To get it, use EventQueue.getCurrentEvent(). For example if user has triggered the change on clicking of another component, you can compare the component of the input event and the component of the action event (OK I know: it's unsafe. But in some cases it can help to avoid incrementing of application complexity).

Autres conseils

For a button you can get the event modifiers: int buttonModifiers = evt.getModifiers(); If the button event was generated with a call to doClick() the modifier is 0, otherwise not.

By the way, you can find out such differences relatively easy by logging / printing using evt.toString() .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top