JavaFX TabPane: How to set the selected tab JavaFX TabPane: How to set the selected tab java java

JavaFX TabPane: How to set the selected tab


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 objectselectionModel.select(1); //select by index starting with 0selectionModel.clearSelection(); //clear your selection

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


To simplify the above mentioned approach:

myTabPane.getSelectionModel().select(myTab);


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