2 Replies Latest reply: Jun 21, 2011 4:22 PM by 839572 RSS

    How to change the scene

    839572
      Hello,

      I'm trying to understand how I can change easily the current scene of an application with javafx 2.
      The idea is to build different scene. For example a login scene and switch to the main scene of the application after certain event.
      Does anybody know a small tutorial about that? Or a small example?

      Thx for your time.

      Edited by: 836569 on 21-giu-2011 13.38
        • 1. Re: How to change the scene
          shakir.gusaroff
          Here is an example:

          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Group;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.paint.Color;
          import javafx.stage.Stage;
          
          
          public class SetScene extends Application {
          
              
              public static void main(String[] args) {
                  Application.launch(SetScene.class, args);
              }
          
              @Override
              public void start(final Stage primaryStage) {
                  primaryStage.setTitle("Hello World");
                  Group root1 = new Group();
                  Group root2 = new Group();
                  final Scene scene1 = new Scene(root1, 300, 250, Color.LIGHTGREEN);
                  final Scene scene2 = new Scene(root2, 300, 250, Color.LIGHTBLUE);
                  Button btn = new Button();
                  btn.setLayoutX(100);
                  btn.setLayoutY(80);
                  btn.setText("Login");
          
                  javafx.scene.text.Text t = new javafx.scene.text.Text("Success");
                  t.setLayoutX(100);
                  t.setLayoutY(80);
          
          
                  btn.setOnAction(new EventHandler<ActionEvent>() {
          
                      public void handle(ActionEvent event) {
                          primaryStage.setScene(scene2);
                      }
                  });
                  root1.getChildren().add(btn);
                  root2.getChildren().add(t);
                  primaryStage.setScene(scene1);
                  primaryStage.setVisible(true);
              }
          }
          • 2. Re: How to change the scene
            839572
            Thank you for the quick replay.