iceman
iceman

Reputation: 4261

Multiple scenes in JavaFX

I'm writing a very simple application in Javafx where there is a single button with a textbox on the stage as one scene.Now,the behavior I want is that when I click the button I can load another scene with another button and a textbox on the stage and remove the button i clicked alongwith the previous textbox. So the click of a button should load a new scene on the stage. any hints on how i can do this ?

Following Eric's advice:i have this code,and its working the way I want.

var showScene1 = true;
var showScene2 = false;
var showScene3 = false;

def stage = Stage
{
  title: "Hello World"

    var scene1 =Scene
    {
          content:
          [

                                  Text {
                          font : Font {
                                  size: 24
                          }
                          x: 10, y: 30
                          content: "HelloWorld from Scene1"
                  },
                  Button
                      {
                          text: "Click Me to change to Scene2 "
                          onMouseClicked: function( e: MouseEvent ):Void
                          {

                                  showScene2 = true;

                                  println("In scene 2");

                          }

                        }


          ]
     }



     var scene2 =Scene
        {
              content:
              [

                                      Text {
                              font : Font {
                                      size: 24
                              }
                              x: 10, y: 30
                              content: "HelloWorld from Scene2"
                      },
                      Button
                          {
                              text: "Click Me to change to Scene3 "
                              onMouseClicked: function( e: MouseEvent ):Void
                              {
                                      showScene1 = false;
                                      showScene2 = false;
                                      showScene3 = true;
                                      println("In scene 3");

                              }

                            }


              ]
         }

     var scene3 =Scene
        {
              content:
              [

                                      Text {
                              font : Font {
                                      size: 24
                              }
                              x: 10, y: 30
                              content: "HelloWorld from Scene3"
                      }


              ]
         }


scene: bind if (showScene2) then scene2
    else if (showScene1) then scene1
    else scene3

}

Upvotes: 7

Views: 11455

Answers (3)

GeertHabbenJansen
GeertHabbenJansen

Reputation: 1

You can make a secon scene like you did your first one. In the fuction of the first button you replace showScene2 = true; with: stage.setScene(scene2);

Upvotes: 0

Abie
Abie

Reputation: 88

Eliminate all the if-else statements. Bind directly to a variable that contains the current scene.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;

var currentScene: Scene;


def scene1: Scene = Scene {
    content: Text {
        font : Font {
            size : 24
        }
        x: 10, y: 30
        content: "Scene 1"
        onMouseClicked: function( e ):Void {
         currentScene = scene2;
       }
    }
}

def scene2: Scene = Scene {
    content: Text {
        font : Font {
            size : 24
        }
        x: 10, y: 30
        content: "Scene 2"
        onMouseClicked: function( e ):Void {
         currentScene = scene1;
       }
    }
}

Stage {
    title: "Multi-Scene"
    width: 250
    height: 80
    scene: bind currentScene
}

currentScene = scene1;   

Upvotes: 1

Eric Wendelin
Eric Wendelin

Reputation: 44349

If you are sure you will only have 2 different scenes, you can just bind the scene property of the Stage like so:

var showSecondScene = false;
var myButton = Button {
    onMouseClicked: function(e) { showSecondScene = true; }
}
def stage = Stage {
    scene: bind if (showSecondScene) then secondScene else firstScene
}

UPDATE: This actually works if you have any number of scenes like so:

scene: bind if (showScene1) then scene1
    else if (showScene2) then scene2
    else scene3

You might consider why you'd have more than 2 scenes, instead opting for setting 'visible: false' on overlapping Group nodes instead.

Upvotes: 1

Related Questions