Java JTabbedPane allows Tab to switch only if true

I have some checks that I perform when a user clicks on a tab. If validation == true, let the tab display (or switch). I am now using the StateChanged event to validate. The problem is that when you click on a tab, it shows the contents of the tabs, and if validation == false, it switches to the previous tab. I do not want it to switch at all if the validation == is true. How do I do this, am I checking the wrong event? Thanks to everyone.

+5
source share
2 answers

You can use the tab.setEnabledAt (index, false) method to disable the tab if validation = false, and tab.setEnabledAt (index, true) to enable it when validation = true.

Edit: disabling the tab will be grayed out so that the user cannot click on it in the first place, which means that you will need to perform a validation check before the user clicks the tab.

+3
source

Try overriding JTabbedPane.setSelectedIndex (index int).

+1
source

All Articles