Java JTabbedPane questions

南笙酒味 提交于 2019-12-11 06:13:07

问题


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:


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




回答2:


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);
                  }
              }
          }           
      }
}



回答3:


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




回答4:


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.

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.

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




回答5:


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.



来源:https://stackoverflow.com/questions/2016582/java-jtabbedpane-questions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!