java 如何在 JTabbedPane 中检查选项卡是否处于活动状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2937684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether the tab is active or not in JTabbedPane?
提问by Supereme
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?
如何在选项卡(嵌套类)本身的类中而不是在封闭类中检查 JTabbedPane 实例中的选项卡是否处于活动状态?
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).
我知道有一个方法,booloean isEnabledAt(int index);但是这个方法只能在封闭的类中调用。而我想检查当前是否在选项卡类本身(嵌套类)中选择了选项卡。
Can anybody please suggest how?
任何人都可以请建议如何?
回答by miku
Your component has a parent, eventually the JTabbedPane. JTabbedPanehas methods like getSelectedIndex()or getSelectedComponent().
您的组件有一个父级,最终是JTabbedPane. JTabbedPane有像getSelectedIndex()或这样的方法getSelectedComponent()。
回答by Bulit
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 ChangeEventlistener. 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.
要确定选择了哪个选项卡,请使用ChangeEvent侦听器。这是在选择选项卡时执行操作的一种非常简单的方法。希望这会对其他人有所帮助,尽管这是一个古老的话题。
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 ....
}
}

