调用 JTabbedPane.removeAll() 后,JTabbedPane 仍然有 x 个选项卡? [英] After calling JTabbedPane.removeAll(), the JTabbedPane still has x number of tabs?

查看:37
本文介绍了调用 JTabbedPane.removeAll() 后,JTabbedPane 仍然有 x 个选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 JTabbedPane 中,我以两种不同的方式删除标签:

In my JTabbedPane, I am removing tabs in 2 different ways:

tabbedPane.remove(index)

tabbedPane.removeAll()

在关闭选项卡方面都可以正常工作.但是,我的 TabbedPane 上有一个更改侦听器,它回调到另一个模块以报告选项卡更改.这就是问题所在.

Both work fine in terms of closing the tabs. However, I have a change listener on my TabbedPane that calls back to another module to report on tab changes. This is where the problem is.

使用 remove(index) 添加和删除选项卡时,stateChanged() 方法中的源 TabbedPane 在检查 tabbedPane 时包含正确数量的选项卡.getTabCount().

When adding and removing tabs using remove(index), the source TabbedPane in the stateChanged() method contains the correct number of tabs when checking tabbedPane.getTabCount().

但是,在 tabbedPane.removeAll() 之后调用 tabbedPane.getTabCount() 时,计数仍然是 removeAll().

However, when calling tabbedPane.getTabCount() after tabbedPane.removeAll(), the count is still the count that was present immediately before the removeAll().

大家有什么建议吗?

推荐答案

查看源代码后,我明白发生了什么.

After looking at the source code, I see what's happening.

JTabbedPane 在选定的选项卡更改时触发 ChangeEvents.但是要删除所有选项卡,它首先将所选选项卡设置为 -1,然后 然后 删除所有选项卡.因此,当 ChangeListener 捕获到事件时,所有选项卡仍然存在.

JTabbedPane fires ChangeEvents when the selected tab is changed. But to remove all tabs, it first sets the selected tab to -1 and then removes all the tabs. So when the ChangeListener catches the event, all the tabs are still there.

如果你想随时知道标签的数量,恐怕你必须自己遍历标签,并把它们一一删除.

If you want to know the number of tabs at all times, I'm afraid you'll have to iterate through the tabs yourself and remove them one by one.

while (myTabbedPane.getTabCount() > 0)
    myTabbedPane.remove(0);

这篇关于调用 JTabbedPane.removeAll() 后,JTabbedPane 仍然有 x 个选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆