Reputation: 311
My question is about the same as these two:
-JavaFX how to inject new FXML content to current Scene
-set the content of a anchorPane with fxml file
I could not use these answers because I don't understand both of them. I have two (and later more) FXML files where one functions as a 'main', which has a pane where the contents of the other FXML file should be added to.
How should I implement this?
Upvotes: 0
Views: 1520
Reputation: 311
Program entry point:
public class App extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent startScreen
= FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(startScreen);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
MainScreenController
public class MainScreenController implements Initializable{
private static AnchorPane contentBox;
@FXML
private AnchorPane paneContent;
public MainScreenController() throws IOException {
this.paneContent = FXMLLoader.load(getClass().getResource("Home.fxml"));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
MainScreenController.contentBox = this.paneContent;
}
public static AnchorPane getContentBox(){
return MainScreenController.contentBox;
}
}
Then MainScreen.fxml needs to have MainScreenController as controller and also needs to contain an AnchorPane with fx:id paneContent. Then from anywhere in your program you can call getContentBox() and use .set() to change the screen.
Upvotes: 2