JavaFX TabPane:如何设置选定的选项卡

发布于 2024-11-27 03:38:35 字数 421 浏览 1 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

下壹個目標 2024-12-04 03:38:35

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

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

将其存储在某个局部变量中后,您可以使用不同的选项来选择选项卡。

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

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

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.

凹づ凸ル 2024-12-04 03:38:35

为了简化上述方法:

myTabPane.getSelectionModel().select(myTab);

To simplify the above mentioned approach:

myTabPane.getSelectionModel().select(myTab);
山人契 2024-12-04 03:38:35

要继续 Menai 的答案,请参阅如何重新聚焦打开的选项卡/TabPane。

SingleSelectionModel<Tab> selectionModel = TabPane.getSelectionModel();
if(!Tabpane.getTabs().contains(tabName)) {
   TabPane.getTabs().add(tabName);
   selectionModel.select(tabPane);
} else {
   selectionModel.select(tabPane); 
}

To continue with Menai's answer heres how to refocus the opened tab/TabPane.

SingleSelectionModel<Tab> selectionModel = TabPane.getSelectionModel();
if(!Tabpane.getTabs().contains(tabName)) {
   TabPane.getTabs().add(tabName);
   selectionModel.select(tabPane);
} else {
   selectionModel.select(tabPane); 
}
垂暮老矣 2024-12-04 03:38:35
TabPane tabPane = new TabPane();

tabPane.getSelectionModel().select(); //Select Tab
tabPane.getSelectionModel().select(); //Select tab index
tabPane.getSelectionModel().selectLast();
tabPane.getSelectionModel().selectFirst();
tabPane.getSelectionModel().selectNext();
tabPane.getSelectionModel().selectPrevious();
tabPane.getSelectionModel().clearSelection(); //Select Tab
tabPane.getSelectionModel().clearSelection(); //Select tab index
TabPane tabPane = new TabPane();

tabPane.getSelectionModel().select(); //Select Tab
tabPane.getSelectionModel().select(); //Select tab index
tabPane.getSelectionModel().selectLast();
tabPane.getSelectionModel().selectFirst();
tabPane.getSelectionModel().selectNext();
tabPane.getSelectionModel().selectPrevious();
tabPane.getSelectionModel().clearSelection(); //Select Tab
tabPane.getSelectionModel().clearSelection(); //Select tab index
你丑哭了我 2024-12-04 03:38:35

如果您使用statique选项卡,我的意思是您的TabPane具有静态数量的选项卡,您可以通过以下方式选择您的选项卡:

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

        selectionModel.select(tabName);

如果您使用dynamique选项卡,我的意思是您的TabPane 具有动态数量的选项卡(添加和删除选项卡),您可以通过以下方式选择选项卡:

        if (!TabPane.getTabs().contains(tabName)) {

        TabPane.getTabs().add(tabName);

    }
    SingleSelectionModel<Tab> selectionModel = TabPane.getSelectionModel();

    selectionModel.select(tabPane);

If you work with statique tabs ,i mean your TabPane has statique number of tabs ,you can select your tab by this way :

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

        selectionModel.select(tabName);

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 :

        if (!TabPane.getTabs().contains(tabName)) {

        TabPane.getTabs().add(tabName);

    }
    SingleSelectionModel<Tab> selectionModel = TabPane.getSelectionModel();

    selectionModel.select(tabPane);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文