java - 停用侦听器

发布于 2024-11-25 17:12:59 字数 473 浏览 8 评论 0原文

我有一个关于听众的一般性问题。

假设我有两个 JTabbedPane,并且都有一个 ChangeListener。它们都显示出来,我希望它们都显示相同的窗格(索引),这样当用户更改其中一个窗格中的选定窗格时,其他窗格也会更改。
简而言之,一个 JTabbedPane 侦听器使用 setSelectedTab() 更改另一个 JTabbedPane
显然,第一个侦听器将激活第二个侦听器,第二个侦听器将在无限操作中重新激活第一个侦听器。

这将通过布尔值来解决。
有更聪明的方法吗?
有没有办法在不触发监听器的情况下更改选项卡?
有没有办法仅在用户更改侦听器而不是代码时激活侦听器?

谢谢。

顺便说一句:我总是对按钮有同样的疑问。但使用按钮时,我从侦听器获取代码并将其放入方法中。当“一个按钮”需要激活一个按钮时,它会调用其代码。但在 JTabbedPane 中情况有所不同。

I have a general question regarding listeners.

Lets say I have two JTabbedPanes 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 技术交流群。

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

发布评论

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

评论(2

献世佛 2024-12-02 17:13:00

简单的解决方案是仅在必要时采取行动。例如:

if(currentTab != desiredTab) {
  // change tab
}

这将防止无限循环。

如果您需要能够打开和关闭该行为,那么使用布尔标志并不是一个坏方法。另一种方法是使用 删除ChangeListener。该标志可能会更具性能,因为它可以避免内存分配和释放,但很大程度上取决于您情况的其他细节。

The simple solution is to act only when necessary. For example:

if(currentTab != desiredTab) {
  // change tab
}

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.

又怨 2024-12-02 17:13:00

分享选择模型,例如

 secondTabbedPane.setModel(otherTabbedPane.getModel());

share the selectionModel, like

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