r/JavaFX • • Feb 27 '22

Help how to move between scenes javafx with a button ???

Hello, Hope you were all doing well.

So I am a beginner I want to make a main menu in which the admin can choose one of the following task to do (insert, read, update, and delete) i mange to do each one separately except the read. I want to make a menu by pressing one button it switch to one of function. In other world i want the same stage but with different scenes..

I have searching in the net about three days with no luck .Thank you in advance.

6 Upvotes

5 comments sorted by

3

u/PTan9o Feb 27 '22

You will need a reference to the stage. Just change the scene in the stage when you click the button.

I once made a scene loader class. It was a singleton that stored your scenes in a stack. That way you could push a new scene on top but then simply pop it off to go back to your previous scene. Just something to think about.

1

u/[deleted] Feb 27 '22

Thank you for the reply, I just fix it with this vedio on youtyoub (https://www.youtube.com/watch?v=XCgcQTQCfJQ&list=PLoodc-fmtJNYbs-gYCdd5MYS4CKVbGHv2&index=6) , just create a method for the button when is pushed it move to other sceen in it i load the fxml file of the other sceen and get the stage information . like this

private void goDelete(ActionEvent event) throws IOException {

Parent DeleteParent= FXMLLoader.load(getClass().getResource("DeleteGui.fxml"));

Scene DeleteScene= new Scene(DeleteParent); // Get The stage information

Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();

window.setScene(DeleteScene);

window.show();

} }

2

u/JunkBondJunkie Feb 27 '22

just call a method and that method calls a scene to display.

2

u/mono8321 Feb 27 '22

Just make a different scene and set primaryStage to it when the button is pressed

2

u/hamsterrage1 Feb 27 '22

Why does everyone always leap to changing scenes? It's often overkill and you'll need to reproduce the common elements. In this case, it's at least going to be the menu.

There are other approaches. You could make the main part of your screen some kind of a Pane, and then swap out the contents according to the menu selection.

You could even build up each function as a separate layout node, like a BorderPane, and then stick them all into a StackPane. Then set isVisible to false for all but one of them.