Reputation: 647
I'm using a JTabbedPane with 3 tabs and Netbeans 7.3. When I open up the Pane, it opens to the first tab, as it should. However if I try to switch to the third tab, the second tab's Components are displayed, even though in the ChangeListener for the JTabbedPane, jTabbedPane.getSelectedIndex() == 2,
as it should.
The third tab's components will only display if you switch to the second tab and then open the third tab. After that, behavior is normal.
The closest thing I could find was this error: JTabbedPane.getTabComponentAt(int) returning null
Has anyone encountered something like this before? I worked around it by "visiting" the second tab in the constructor.
Upvotes: 1
Views: 303
Reputation: 41
I had the same symptoms as those described by the OP -- components from one JTabbedPane
tab were "bleeding through" to another tab. Although I can't be sure it was caused by the same thing since the OP never complied with the request to post an SSCCE, I thought I would post the following in case others come across this in the future.
The source of my problem was that I was invoking setVisible()
on components which had been added to the JTabbedPane
. I figured this out after a lengthy search brought me to https://bugs.openjdk.java.net/browse/JDK-6191978. According to that bug report, JTabbedPane
makes internal use of the notion of visibility, and changing it externally confuses the tab manager. Components in a JTabbedPane
should never have their setVisible()
method invoked; rather, the user should invoke setSelectedComponent()
or setSelectedIndex()
to make the tab contents appear. (This is now documented in the JDK API docs, but it wasn't back when the code was originally written...)
Hope this saves someone the hours of searching and debugging I did...
Upvotes: 4