JavaFX TabPane:如何设置选定的选项卡
我有一个包含 JavaFX 2 的 Java 桌面应用程序,在我的 FX 中我有一个 TabPane。我想设置默认选项卡。换句话说,我想将选项卡设置为选定状态。我发现有多种方法可以找出选择了哪个选项卡,并且我找到了 setSelectionModel()
但我不知道如何使用它。
TabPane tabPane = new TabPane();
Tab tab0 = new Tab("blue");
tab.setContent(new Rectangle(200,200, Color.BLUE));
Tab tab1 = new Tab("green");
tab.setContent(new Rectangle(200,200, Color.GREEN));
tabPane.getTabs().addAll(tab0, tab1);
I have a Java Desktop Application with JavaFX 2 in it and in my FX I've got a TabPane. I want to set the default tab. In other words I want to set a tab as selected. I found that there are multiple ways to find out which tab is selected and I found setSelectionModel()
but I can't figure out how to use it.
TabPane tabPane = new TabPane();
Tab tab0 = new Tab("blue");
tab.setContent(new Rectangle(200,200, Color.BLUE));
Tab tab1 = new Tab("green");
tab.setContent(new Rectangle(200,200, Color.GREEN));
tabPane.getTabs().addAll(tab0, tab1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SelectionModel
是正确的方法。您可以从TabPane
获取默认值,或使用setSelectionModel(...)
分配您自己的实现。默认模型对于开始来说应该足够好了。将其存储在某个局部变量中后,您可以使用不同的选项来选择选项卡。
如果您尝试选择一个不存在的选项卡,则不会发生任何情况。
The
SelectionModel
is the right approach. You can get the default from yourTabPane
or assign your own implementation by usingsetSelectionModel(...)
. The default model should be good enough for the beginning.Once you stored it in some local variable, you have different options to select a tab.
If you try to select a non existing tab, nothing will happen.
为了简化上述方法:
To simplify the above mentioned approach:
要继续 Menai 的答案,请参阅如何重新聚焦打开的选项卡/TabPane。
To continue with Menai's answer heres how to refocus the opened tab/TabPane.
如果您使用statique选项卡,我的意思是您的TabPane具有静态数量的选项卡,您可以通过以下方式选择您的选项卡:
如果您使用dynamique选项卡,我的意思是您的TabPane 具有动态数量的选项卡(添加和删除选项卡),您可以通过以下方式选择选项卡:
If you work with statique tabs ,i mean your TabPane has statique number of tabs ,you can select your tab by this way :
If you work with dynamique tabs ,i mean your TabPane has dynamique number of tabs (add and remove tabs) ,you can select your tab by this way :