Reputation: 2000
I'm trying to implement an App with JavaFX and SceneBuilder.
I made a Main menu with different buttons, and each button leads to a different page.
So for example, when I click the first button, i want that i switch to a new panel and then, if I want to go back to the previous panel, I have just to click on "Back"-Button on the 2nd panel that I got.
The 1st step, to have a new page when I click on the buttons, is okay. Now the problem is in implementing the "Back" Button. I want to click on it, and have the previous panel, but I dont find a solution for that.
Here is the code:
start method of my App (OrderCheck.java)
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("OrderCheck.fxml"));
Pane myPane = (Pane)myLoader.load();
primaryStage.setTitle("Main Menu");
OrderCheckController controller = (OrderCheckController) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
example of the onAction of 1 button on the main panel: (OrderCheckController.java):
public void personalButtonAction(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Personal Menu");
AnchorPane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("Personal.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
}
Example of implementation of the Back-Button in the new view, when that button is clicked (PersonalController.java):
public void backButtonAction(ActionEvent event) throws IOException {
Stage stageTheLabelBelongs = (Stage) backButton.getScene().getWindow();
setPrevStage(stageTheLabelBelongs);
prevStage.close();
stageTheLabelBelongs.show();
}
the method setPrevStage(Stage stage) is implemented in both Controllers (OrderCheckController and PersonalController)
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
So now when I click on the "Back"-button on the new page that i get, the window will be fast closed and opened-up again as nothing happened.
Upvotes: 2
Views: 5545
Reputation: 373
If you have Main class like user Sudip Saha then just make method goBack() in controller where you are using Back button.
goBack() method:
public void goBack() {
BorderPane mainScreen;
try {
mainScreen = (BorderPane) FXMLLoader.load(JavaFXApplication.class .getResource("../javafx/main.fxml"));
JavaFXApplication.setCenterPane(mainScreen);
} catch (IOException e) {
e.printStackTrace();
}
}
After that just call goBack() method in "Back" button in Scene Builder
Upvotes: 1
Reputation: 310
Try like this. I refer to the primary stage this way in my app.
public class JavaFXApplication extends Application {
public static Stage primaryStage;
@Override
public void start(Stage stage) {
stage.setTitle("Hello World!");
stage.setScene(scene);
stage.show();
primaryStage = stage;
}
public static void main(String[] args) {
launch(args);
}
}
And from your other controllers you can access it using JavaFXApplication.primaryStage
Upvotes: 2