JavaFX TabPane: How to set the selected tab
Asked Answered
T

5

59

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);
Thursday answered 1/8, 2011 at 17:56 Comment(0)
Z
118

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.

Zenaidazenana answered 2/8, 2011 at 17:49 Comment(0)
K
37

To simplify the above mentioned approach:

myTabPane.getSelectionModel().select(myTab);
Knecht answered 22/7, 2013 at 16:52 Comment(1)
It really helps.Forspent
P
1

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); 
}
Practise answered 12/3, 2018 at 1:48 Comment(0)
M
1
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
Moffit answered 14/11, 2021 at 9:56 Comment(0)
M
-1

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);
Marlo answered 14/8, 2017 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.