views:

480

answers:

3

I'm using a JTabbedPane to hold each step in the wizard that I am building. Navigation between steps is doing using the Previous/Next buttons or by selecting a tab. The buttons decrement/increment the JTabbedPane's SelectedIndex.

I need to validate each step before proceeding to the next step. Essentially, I'm having difficulty determining which event to use. The StateChange event occurs too late. Which event do I need to observe?

Another irritation: when the wizard runs, it seems to save the state of the JTabbedPane's SelectedIndex (usually the last step's value), this value is then used to set the SelectedIndex the next time the wizard is run. The selectedIndex property in the designer hasn't change. Moreover, calling the setSelectedIndex() in the JPanel's contructor doesn't seem to have an effect on this. What am I missing?

+1  A: 

First, you're using a JTabbedPane, which indicates that the user can select tabs in any order, to implement a wizard, which typically requires steps to be done in sequential order. Think about whether this is the right UI component to use.

Second, what you're looking for is a vetoable state change, although that doesn't exist out-of-the-box.

I found this in a thread in Sun's forums:

The trick I've used is to replace the SingleSelectionModel which is used by the JTabbedPane. Extending the DefaultSingleSelectionModel and overiding the setSelectedIndex method seems to do the job fine (calling the super method only if the switch is to be permitted).

Also, Kirill Grouchnikov has some thoughts here - "Spicing up your JTabbedPane - part V".

David
Thanks for your help.
Craig
+1  A: 

It occurs too late because that's fired when the tab have already changed, when what you need is to know when it is about to change.

What you could try ( if you insist using the tabbed pane which may not be the best option ) is to add a mouse listener and use it in conjunction with a glass pane. That will capture the mouse event and will allow you to perform the validation. If it succeeds you change the tab programatically.

alt text

You'll have to wire the events, which makes the code a bit difficult to write ( that's why you don't see tabs in wizards I guess )

As for the index, that's because you're using the same instance. It doesn't have effect because you have to invoke it on the instance not in the constructor.

Here's a sample on how to use GlassPanes.

alt text

See how in the sample they intercept the click event on the button. You could try something similar with tour tab.

OscarRyz
+3  A: 

Instead of JTabbedPane, consider using CardLayout. The latter lets you control navigation more rigidly.

trashgod
The CardLayout works well. Two related questions. 1: Is there a way to show tabs? 2: Which event can I use to validate the entry on each card?
Craig
"Which event can I use to validate the entry on each card?" - when the user presses the Next button, make validation happen in the actionPerformed() method, and if validation fails, don't change the panel.
David
"Is there a way to show tabs?" - I don't think you want to show tabs, but I think you may want to show a list of steps, and indicate which step the user is working on. I'd recommend a vertical panel on the left side of the wizard panel, JLabels for each step, and use bold to highlight the current step.
David
David's comments are very cogent. The real question isn't "What component should I use?" but "How can I guide the user to make coherent choices?" It may help to create a model with suitable defaults so the user doesn't get "stuck" with no clear idea of what's wrong. Try to create "preferences" rather than "sequences".
trashgod