user1861088
user1861088

Reputation: 1733

Catch event when a JTabbedPane tab is clicked (before actual state change)

I know I can add a ChangeListener to detect when the tab changed, but I would like to do it before it changed. Here's my situation:

when my user tries to switch tab I do a validation some content on the current tab and the tab should switch only if the validation passes. If I use addChangeListener and switch it back upon validation failure (hoping that it happens fast enough that I won't see it) I will see the tab switched and then immediately switched back which looks stupid.

Something within JTabbedPane is responding to tab click for sure that triggers a state change, I just wonder if this functionality is exposed outside of the class. Either way, is there a good solution for this?

Thanks in advance

Upvotes: 1

Views: 1522

Answers (1)

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

Here is a nice approach to do validation before switching tabs by creating a custom VetoableSingleSelectionModel:

Forbid tab change in a JTabbedPane

Upvotes: 2

Related Questions