English 中文(简体)
Java JTabbedPane questions
原标题:

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?

最佳回答

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

问题回答

You can extends JTabbedPane to override the setSelectedIndex method and then call super.setSelectedIndex(index); after doing your validation something like:

public class ValidationTabbedPane extends JTabbedPane {
    private static final long serialVersionUID = 1L;

    public ValidationTabbedPane() {
        super();
      }

      @Override
      public void setSelectedIndex(int index) {
          if (index >= 0 && index < getTabCount()) {
              if(getSelectedIndex() == -1) {
                  super.setSelectedIndex(index);
              } else  {
                  Containers tab = tabs.get(getSelectedIndex());
                  boolean change = true;
                  if(tab.isDirty()) {
                      if(!MessageAlert.
                              yesNoMessage("You have unsaved changes. Do you want to change tab anyways? ", 
                                      mainTabbedPane)) {
                          change = false;
                      }
                  }
                  if(change) {
                      super.setSelectedIndex(index);
                  }
              }
          }           
      }
}

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".

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.

A sleazy alternative (which is what i m going to do because I ve already done most of the and am averse to drastic changes now) is to set the enabled property to false. The tab buttons will be disabled but your next and previous buttons will still behave as expected. If your users will tolerate this it may be the easy solution.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签