Tombart
Tombart

Reputation: 32428

Java Swing: How to distinguish events triggered by user?

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")

Upvotes: 2

Views: 529

Answers (2)

Günter Fuchs
Günter Fuchs

Reputation: 21

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() .

Upvotes: 0

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

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).

Upvotes: 2

Related Questions