tags:

views:

145

answers:

3

Under certain circumstances, I need a JTabbedPane to remain on one pane until the user supplies certain information. Essentially, when this circumstance occurs, I need the current pane to become modal.

How can I implement this? I was thinking I could catch whatever event is triggered when the pane changes, and reset back to the pane I want to stick on. But I'm worried that this won't be quite right, that depending on when the event actually fires the transition to the new pane will happen after I call the method to set the pane to the pane I want, or some other similar race condition. Is there a better way? Is there a way I can make this approach work?

+6  A: 

I would suggest setting the other tabs to disabled. This has a positive effect of providing the user feedback that they cannot click out of the tab. Otherwise they may be madly clicking and wondering why it will not let them leave the tab.

Simply set them enabled again after the required fields are completed.

Chris Kannon
Also make it very clear to the user what they need to fix. I can't think of many things more frustrating that being stuck on a modal anything that's waiting for some input I don't know how to enter.
TwentyMiles
@TwentyMiles Yup, I usually highlight the field red or yellow.. make it stand out.
Chris Kannon
This is my accepted answer, but I used pieces of all the others; in particular, it was ChadNC who clued me into the setEnabledAt() method (which I couldn't seem to find on my own, despite being fairly proficient at reading JavaDocs), and Carlos' solution almost worked for me and made a good stopgap solution to get the basic concept working.
skiphoppy
+1  A: 

just disable the JTabbedPane:

pane.setEnabled(false);

and enable it if all fields are correctly set (or whatever condition)

Carlos Heuberger
Pretty cool! Slightly off because it greys out the current tab, but the current pane does stay active, so it works. I think I'll go for trying to disable each individual tab, though, if I can work it out correctly.
skiphoppy
@skiphoppy - if you don't want to grey out the current tab, you can go by @Chris solutions: use `JTabbedPane.setEnablebAt(int, boolean)` to disable each tab but the current.
Carlos Heuberger
+2  A: 

You could use a CardLayout along with JPanels to do what you want and not use JTabbedPanes. Since you need to use the tabbed panes, I would suggest that once the condition has been reached that you want to force the user to stay on that tab set that tab to be the selected one by using.

setTabComponentAt(int index, Component component) 

or

setSelectedIndex(int index) 

Set a flag indicating that the user should not be able to proceed until completing whatever it is you want them to do and have all the other tabs be disabled using setEnabledAt(int index, boolean enabled) . Once the user has completed what they needed in order to continue set the flag accordingly and reenable the other tabs.

I haven't the time to try that solution out but I think it should work.

ChadNC