0

I am programming a login screen with JavaFX 2. It looks pretty well with the oracle documentation

http://docs.oracle.com/javafx/2/get_started/form.htm

But I would like to add a button "Register" that changes the scene with the same window.

I tried with "pane.setVisible(value);" but that didn't work

What is the best way to do this?

Aspirant
  • 1,934
  • 4
  • 25
  • 44
Kaito
  • 316
  • 1
  • 4
  • 14

1 Answers1

2

You have to return a Scene from another method or create a Scene within the "Start", and set the Scene on stage. For example:

public static Scene secondScene(final Stage primaryStage){
     BorderPane pane = new BorderPane();
     Label l = new Label("Second Scene");
     pane.setCenter(l);

      //let's say I have a button that changes back to original stage
     Button b = new Button("Main stage");  
     b.setOnAction(new EventHandler<ActionEvent>(){
     public void handle(ActionEvent t){
           // create your own Scene and then set it to primaryStage
          primaryStage.setScene(new Scene ... );
     }
     });

     return new Scene(pane, 300, 300);
}

Then on your start method:

@Override
public void start(Stage primaryStage){
BorderPane mainPane = new BorderPane();
Button b = new Button("Register");
mainPane.setCenter(b);

b.setOnAction(new EventHandler<ActionEvent>(){
     public void handle(ActionEvent t){
          primaryStage.setScene(secondScene(primaryStage));
     }
});

  primaryStage.setScene(new Scene(mainPane, 300,300));
  primaryStage.show();
}

This will change to "SecondScene" when the button is pressed.

EDIT //////////////////////////////////////// BASICALLY:

you need to pass in primaryStage as an argument to SecondScene in order to set another Scene onto it. What you could do in this case is, make a method that outputs the main scene and then set it using that.

public static Scene mainScene(final Stage primaryStage){
     .....
     return new Scene(...);
}
Stevantti
  • 494
  • 2
  • 6
  • 13
  • thank you :) that is already very useful, but how do I then change back to the first scene? :o because I can't use "primaryStage.setScene(secondScene);" because it is an unknown variable – Kaito Dec 10 '13 at 07:46
  • if it helped you, pressed the check mark :) – Stevantti Dec 10 '13 at 09:14