9

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?

Leigh
  • 28,765
  • 10
  • 55
  • 103
Supereme
  • 2,379
  • 12
  • 46
  • 67

2 Answers2

16

Your component has a parent, eventually the JTabbedPane. JTabbedPane has methods like getSelectedIndex() or getSelectedComponent().

miku
  • 181,842
  • 47
  • 306
  • 310
  • Thank u. But How can I call these methods in the nested class of JTabbedPane? as the need is to see whether a tab is selected or not and if it is I have to update the contents by removing old version of a component and replacing it with new one. How to achieve it? – Supereme May 30 '10 at 10:02
7

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 ....
    }
} 
Leigh
  • 28,765
  • 10
  • 55
  • 103
Bulit
  • 975
  • 6
  • 15
  • 26