Answer the question
In order to leave comments, you need to log in
Javafx - how to call the rest from the main controller?
Good afternoon!
I am working on a JAVAFX project. There are several tabs.
Each tab has its own form. Created a separate controller for each tab. The tab bar is launched through the main controller. I want to make it so that controllers for each tab are called in the main controller. I can't figure out how to do it at all. Tell me please.
Here is the part of the code to call the forms in the MainApp class
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the tab overview inside the root layout.
*/
public void showTabOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/TabLayout.fxml"));
AnchorPane tabOverview = (AnchorPane) loader.load();
rootLayout.setCenter(tabOverview);
MainController mainController = loader.getController();
mainController.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public class MainController implements Initializable {
private MainApp mainApp;
public MainController() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("App started");
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
userTable.setItems(mainApp.getPersonData());
}
Answer the question
In order to leave comments, you need to log in
Specifically, I didn’t do such tricks on the main window, but I think something similar can be done there.
In particular, declare the necessary tabs in the controller (like the Tab class), specify the loader object of each controller as a child for the desired tab.
Well, or re-declare the AnchorPane located in the tab on (AnchorPane) loader.load(); (Although this is completely crooked)
I will not say that these are optimal solutions, but at least I indicated in which direction to dig.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question