我使用 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?
发布评论
评论(5)
考虑使用 CardLayout。后者可以让您更严格地控制导航。
Instead of JTabbedPane, consider using CardLayout. The latter lets you control navigation more rigidly.
您可以扩展 JTabbedPane 来重写 setSelectedIndex 方法,然后调用 super.setSelectedIndex(index);进行验证后,如下所示:
You can extends JTabbedPane to override the setSelectedIndex method and then call super.setSelectedIndex(index); after doing your validation something like:
首先,您使用 JTabbedPane(它指示用户可以按任意顺序选择选项卡)来实现向导,这通常需要按顺序完成步骤。考虑一下这是否是正确使用的 UI 组件。
其次,您正在寻找的是可否决的状态更改,尽管这并不存在现成的。
我在Sun 论坛的一个帖子中找到了这个:
另外,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:
Also, Kirill Grouchnikov has some thoughts here - "Spicing up your JTabbedPane - part V".
它发生得太晚了,因为它是在选项卡已经更改时触发的,而您需要知道它何时将要更改。
您可以尝试的(如果您坚持使用选项卡式窗格,这可能不是最好的选择)是添加鼠标侦听器并将其与玻璃窗格结合使用。这将捕获鼠标事件并允许您执行验证。如果成功,您可以通过编程方式更改选项卡。
您必须连接事件,这使得代码编写起来有点困难(我猜这就是为什么您在向导中看不到选项卡的原因)
至于索引,那是因为您正在使用同一个实例。它没有效果,因为您必须在实例上而不是在构造函数中调用它。
以下是有关如何使用 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.
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.
一个简单的替代方案(这就是我要做的,因为我已经完成了大部分工作并且现在不愿意进行大幅度的更改)是将
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.