java - 停用侦听器
我有一个关于听众的一般性问题。
假设我有两个 JTabbedPane,并且都有一个 ChangeListener。它们都显示出来,我希望它们都显示相同的窗格(索引),这样当用户更改其中一个窗格中的选定窗格时,其他窗格也会更改。
简而言之,一个 JTabbedPane 侦听器使用 setSelectedTab()
更改另一个 JTabbedPane
。
显然,第一个侦听器将激活第二个侦听器,第二个侦听器将在无限操作中重新激活第一个侦听器。
这将通过布尔值来解决。
有更聪明的方法吗?
有没有办法在不触发监听器的情况下更改选项卡?
有没有办法仅在用户更改侦听器而不是代码时激活侦听器?
谢谢。
顺便说一句:我总是对按钮有同样的疑问。但使用按钮时,我从侦听器获取代码并将其放入方法中。当“一个按钮”需要激活一个按钮时,它会调用其代码。但在 JTabbedPane 中情况有所不同。
I have a general question regarding listeners.
Lets say I have two JTabbedPane
s and both have a ChangeListener
. They are both displayed and I want them both to show the same pane (index) so when a user changes the selected pane in one the other changes too.
In brief, one JTabbedPane listener changes the other JTabbedPane
using setSelectedTab()
.
Obviously, the first listener will activate the second listener and the second will reactivate the first in an endless operation.
This will be solved with booleans.
Is there a smarter way to do it?
Is there a way to change a tab without triggering the Listener?
Is there a way to activate the listener only when a user changes it and not the code?
Thank you.
BTW: I always have the same questions with buttons. But with buttons I take the code from the listener and put it in a method. when One button needs to activate a button it calls its code. But in JTabbedPane it is different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的解决方案是仅在必要时采取行动。例如:
这将防止无限循环。
如果您需要能够打开和关闭该行为,那么使用布尔标志并不是一个坏方法。另一种方法是使用 删除ChangeListener。该标志可能会更具性能,因为它可以避免内存分配和释放,但很大程度上取决于您情况的其他细节。
The simple solution is to act only when necessary. For example:
That will prevent an infinite loop.
If you need to be able to switch the behavior on and off, then using a boolean flag isn't a bad way to go about it. The alternative is the remove the listener, using removeChangeListener. The flag may be more performant as it may avoid memory allocation and deallocation, but a lot depends on the other details of your situation.
分享选择模型,例如
share the selectionModel, like