JavaFX TabPane: How to set the selected tab

后端 未结 4 1818
一向
一向 2021-02-02 05:50

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 th

4条回答
  •  天涯浪人
    2021-02-02 06:31

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

     SingleSelectionModel 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 selectionModel = TabPane.getSelectionModel();
    
        selectionModel.select(tabPane);
    

提交回复
热议问题