4 Replies Latest reply: Nov 30, 2012 5:46 AM by 976866 RSS

    How can I display a new scene in JavaFX 2.2? For example after button click

    976866
      how to display new scene after button click in the main window, I want the main window and the new scene are in one stage. thx
        • 1. Re: How can I display a new scene in JavaFX 2.2? For example after button click
          jsmith
          You can change the scene by calling stage.setScene(new Scene(newContentParent));

          I don't think you are quite asking for a complete scene change though as you "want the main window and the scene in one stage". The main window is a stage (as Stage extends Window). And a given stage can only contain one scene at a time (though you can swap it out by calling setScene as described earlier).

          What I think you are really asking for how can you replace some content part of the active Scene on the Stage. To do that you can set a layout manager like a HBox or a BorderPane as the root of your scene, then change out the content of the layout manager. For example:
          final BorderPane layout = new BorderPane();
          layout.setCenter(new Label("Dogbert");
          
          final Button nav = new Button("Next");
          layout.setLeft(nav);
          nav.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent actionEvent) {
              layout.setCenter(new Label("Dilbert"));        
            }
          });
          In the above short sample code, if you wanted to change the scene rather than the a pane, then you would call stage.setScene rather than layout.setCenter.

          There is a complete executable example with multiple content panes and some styling here:
          http://stackoverflow.com/questions/13556637/how-to-have-menus-in-java-desktop-application
          • 2. Re: How can I display a new scene in JavaFX 2.2? For example after button click
            abg
            Hi

            I have an energy saving application which can be run from

            http://homepage.ntlworld.com/alex.gardiner/eco-wand/demoalone.html

            The Display combobox selects what is shown. Each "scene" is a Group which has Groupxxx.setVisible(true) when required.

            Alex

            Edited by: abg on 29-Nov-2012 01:24
            • 3. Re: How can I display a new scene in JavaFX 2.2? For example after button click
              976866
              thanks jsmith, I have set a layout manager using a BorderPane as the root of my scene, for example

              public class MyApplication extends Application {

              private BorderPane layout;
              private Scene scene;
              @Override
              public void start(Stage stage) {

              //Create BorderPane layout manager.
              layout = new BorderPane(); //This is the "root node".

              //Give Root Node a CSS ID Attribute
              layout.setId("appContainer");


              //Set Scene Properties.
              setSceneProperties();

              //Build Demo App Layout
              buildLeft();
              buildTop();


              //Set a few properties of our Application Window
              stage.setScene(scene);
              stage.setTitle("SIM-G");
              stage.show();
              }

              * This is VBox containing all buttons.
              */
              public void buildLeft() {

              BorderPane leftLayout = new BorderPane();
              ..............

              //Add new project Buttons
              Button btnExample1 = new Button();

              //Set Button Text
              btnExample1.setText("New Project");

              //Set All Buttons to the same size.
              btnExample1.setMaxWidth(Double.MAX_VALUE);

              //Add Click Event.
              btnExample1.setOnAction(new EventHandler<ActionEvent>() {

              @Override
              public void handle(ActionEvent event) {
              System.out.println("wroking area Button Clicked.");
              layout.setCenter(workingarea());
              }
              });
              • 4. Re: How can I display a new scene in JavaFX 2.2? For example after button click
                976866
                it's nice application abg, thanks for showing me the example