How to check whether the tab is active or not in JTabbedPane?

☆樱花仙子☆ 提交于 2019-12-23 08:18:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!