In my JTabbedPane, I am removing tabs in 2 different ways:
tabbedPane.remove(index)
and
tabbedPane.removeAll()
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.
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().
However, when calling tabbedPane.getTabCount() after tabbedPane.removeAll(), the count is still the count that was present immediately before the removeAll().
Does anyone have any suggestions?
After looking at the source code, I see what’s happening.
JTabbedPanefiresChangeEventswhen 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 theChangeListenercatches 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.