简体   繁体   中英

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

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.

Try override JTabbedPane.setSelectedIndex(int index).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM