问题
How can I check whether a tab in a JTabbedPane instance is active or not, in the class of the tab (nested class) itself and not in the enclosing class?
I know that there is a method booloean isEnabledAt(int index);
but this method can only be called in the enclosing class. Whereas I want to check whether the tab is currently selected within the tab class itself (nested class).
Can anybody please suggest how?
回答1:
Your component has a parent, eventually the JTabbedPane
. JTabbedPane
has methods like getSelectedIndex() or getSelectedComponent().
回答2:
I know that this is old topic, but I found it when looking for a solution to a similar (though slightly different) problem.
To determine what tab was selected, use a ChangeEvent
listener. This is a very simple way to perform an action whenever a tab is selected. Hopefully this will help someone else, though this is an old topic.
private void zakladkiStateChanged(javax.swing.event.ChangeEvent evt)
{
if (zakladki.getTitleAt(zakladki.getSelectedIndex()).equals("tab title here"))
{
// what you wish to do when tab is selected here ....
}
}
来源:https://stackoverflow.com/questions/2937684/how-to-check-whether-the-tab-is-active-or-not-in-jtabbedpane