JavaFX TabPane:如何设置选定的选项卡 [英] JavaFX TabPane: How to set the selected tab

查看:199
本文介绍了JavaFX TabPane:如何设置选定的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有JavaFX 2的Java桌面应用程序,在我的FX中我有一个TabPane。我想设置默认选项卡。换句话说,我想将选项卡设置为选中。我发现有多种方法可以找出选择了哪个选项卡,我找到了 setSelectionModel(),但我无法弄清楚如何使用它。

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);


推荐答案

SelectionModel 是正确的方法。您可以从 TabPane 获取默认值,或使用 setSelectionModel(...)分配您自己的实现。默认模型应该足够开始。

The SelectionModelis the right approach. You can get the default from your TabPane or assign your own implementation by using setSelectionModel(...). The default model should be good enough for the beginning.

SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();

将它存储在某个局部变量中后,您可以选择不同的选项。

Once you stored it in some local variable, you have different options to select a tab.

selectionModel.select(tab); //select by object
selectionModel.select(1); //select by index starting with 0
selectionModel.clearSelection(); //clear your selection

如果您尝试选择不存在的选项卡,则不会发生任何事情。

If you try to select a non existing tab, nothing will happen.

这篇关于JavaFX TabPane:如何设置选定的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆