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
}
In JavaFX, an application can only have one stage but that stage can have 1 or several scenes. Therefore, we can create multiple scenes for a given JavaFX application.
A scene represents the physical contents of a JavaFX application. It contains all the contents of a scene graph. The class Scene of the package javafx. scene represents the scene object. At an instance, the scene object is added to only one stage.
During the loading of your FXML markup, there is only the provision to have one controller specified for your scene graph.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With