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);
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:
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 :
To continue with Menai's answer heres how to refocus the opened tab/TabPane.