user285715
user285715

Reputation: 51

Sequence of the Events in Java

I have two events for two seperate components, but there is a problem. JTabbedPane's stateChanged event is fired before JFormattedField's focusLost event. Is there a way of making stateChange event to be fired after focusLost event.

Thanks, Tuna

Upvotes: 3

Views: 1873

Answers (2)

camickr
camickr

Reputation: 324157

While Java guarantees event will be fired the order is not guaranteed and may differ on various platforms.

A potential solution is to wrap the stateChanged code in a SwingUtilities.invokeLater(). This will place the code at the end of the Event Dispatch Thread (EDT) so it should execute after the focusLost code.

Upvotes: 3

Gnoupi
Gnoupi

Reputation: 4734

From what I checked in the JTabbedPane sources, the fireStateChanged() method triggers a focus change event if necessary, before firing the actual "stateChanged" event to the listeners.

So in theory, it should happen before. However, since I don't know what is happening in the called method (SwingUtilities2.tabbedPaneChangeFocusTo(newComp)), it's highly possible that the event goes into another thread, being related to focus management.

The fireStateChanged() is a protected method, so you could override it in your own JTabbedPane, and make sure the behavior is the one you want.

Give more details about your actual use case, to see if there can be a more appropriate solution.

Upvotes: 3

Related Questions