javafx open a new fxml file in new tab dynamically

2019-09-06 14:39发布

问题:

I am creating a javafx program in which i need to open a new fxml file in new tab dynamically.

I want that When user click on button a new tab is opened with new fxml file.

I Had no idea I can add tab as per design as much as i need but i want to do it dynamically when user click on button then only a new tab open.

I had also seen Questing but not working for me. Please help me.

Thank you.

回答1:

You can add tabs dynamically with

myTabPane.getTabs().add(myNewTab);

Create a new Tab with new Tab(), load your FXML and call

myNewTab.setContent(loadedFxmlRoot);


回答2:

You can add tabs using this code, you need to confirm if your tabPane has this tab or no, else your program invokes SizeOfBounds exception.

if (!MessagePane.getTabs().contains(AllMessageTab)) {
    MessagePane.getTabs().add(AllMessageTab);
}
SingleSelectionModel<Tab> selectionModel = MessagePane.getSelectionModel();
selectionModel.select(AllMessageTab);        
AllMessageTab.setContent(_YourContentNodeHere);