Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JTabbedPane allow Tab to switch to only if true

I have some validation that I perform when the user clicks on a tab. If validation == true then allow the tab to show (or to switch to). Right now I am using the StateChanged event to validate. The problem is, when you click on a tab it shows the tabs contents and if validation == false it switches back to previous tab. I do not want it to switch to at all unless validation == true. How do I do this, am I checking for a wrong event? Thank you all

like image 223
jadrijan Avatar asked Apr 09 '12 14:04

jadrijan


2 Answers

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

Edit: Disabling a tab will grey it out so the user can't click it in the first place, which means you're going to have to perform the validation check before the user click the tab.

like image 163
FljpFl0p Avatar answered Sep 23 '22 13:09

FljpFl0p


Try override JTabbedPane.setSelectedIndex(int index).

like image 38
Zecas Avatar answered Sep 22 '22 13:09

Zecas