Java JTabbedPane 问题

发布于 2024-08-17 08:24:51 字数 379 浏览 7 评论 0 原文

我使用 JTabbedPane 来保存我正在构建的向导中的每个步骤。步骤之间的导航是使用上一个/下一个按钮或通过选择一个选项卡来完成的。这些按钮减少/增加 JTabbedPane 的 SelectedIndex。

在继续下一步之前,我需要验证每个步骤。本质上,我很难确定要使用哪个事件。 StateChange 事件发生得太晚了。我需要观察哪个事件?

另一个令人恼火的是:当向导运行时,它似乎保存了 JTabbedPane 的 SelectedIndex 的状态(通常是最后一步的值),然后该值用于在下次运行向导时设置 SelectedIndex。设计器中的 selectedIndex 属性没有改变。此外,在 JPanel 的构造函数中调用 setSelectedIndex() 似乎对此没有影响。我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洋洋洒洒 2024-08-24 08:24:51

考虑使用 CardLayout。后者可以让您更严格地控​​制导航。

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

何处潇湘 2024-08-24 08:24:51

您可以扩展 JTabbedPane 来重写 setSelectedIndex 方法,然后调用 super.setSelectedIndex(index);进行验证后,如下所示:

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

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);
                  }
              }
          }           
      }
}
旧伤还要旧人安 2024-08-24 08:24:51

首先,您使用 JTabbedPane(它指示用户可以按任意顺序选择选项卡)来实现向导,这通常需要按顺序完成步骤。考虑一下这是否是正确使用的 UI 组件。

其次,您正在寻找的是可否决的状态更改,尽管这并不存在现成的。

我在Sun 论坛的一个帖子中找到了这个:

我使用的技巧是替换 JTabbedPane 使用的 SingleSelectionModel。扩展 DefaultSingleSelectionModel 并覆盖 setSelectedIndex 方法似乎可以很好地完成工作(仅在允许切换时才调用 super 方法)。

另外,Kirill Grouchnikov 这里有一些想法 - “为你的 JTabbedPane 增添趣味- 第五部分”。

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

漫漫岁月 2024-08-24 08:24:51

它发生得太晚了,因为它是在选项卡已经更改时触发的,而您需要知道它何时将要更改。

您可以尝试的(如果您坚持使用选项卡式窗格,这可能不是最好的选择)是添加鼠标侦听器并将其与玻璃窗格结合使用。这将捕获鼠标事件并允许您执行验证。如果成功,您可以通过编程方式更改选项卡。

替代文本

您必须连接事件,这使得代码编写起来有点困难(我猜这就是为什么您在向导中看不到选项卡的原因)

至于索引,那是因为您正在使用同一个实例。它没有效果,因为您必须在实例上而不是在构造函数中调用它。

以下是有关如何使用 GlassPanes 的示例。

替代文本

查看示例中它们如何拦截按钮上的单击事件。您可以尝试使用游览选项卡进行类似的操作。

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.

枫以 2024-08-24 08:24:51

一个简单的替代方案(这就是我要做的,因为我已经完成了大部分工作并且现在不愿意进行大幅度的更改)是将 enabled 属性设置为 false。选项卡按钮将被禁用,但下一个和上一个按钮仍将按预期运行。如果您的用户能够容忍这一点,那么这可能是一个简单的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文