JavaFX dynamic Text content for scene

I'm trying to learn some basic how to's for JavaFX in respect to Java and am confused on how to change the text in a scene as a result of a button press.

This is the code I was trying, but my new text never gets displayed. I haven't found an example on how to do something as basic as this anywhere in JavaFX tutorials.

How can I make the text say "New Scene Text" after clicking the button instead of "Starting Text".
package javafxbutton;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.control.Button;
import java.lang.System;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextOrigin;
import javafx.scene.paint.Color;


Stage {
    title: "Application title"
    scene: Scene {
        width: 250
        height: 120
        content: [
            VBox {
                layoutX: 20
                layoutY: 20
                spacing: 10
                content: [
                    Text {
                        textOrigin: TextOrigin.TOP
                        fill: Color.web("ox131021")
                        font: Font.font("Sans Serif", 14)
                        content: "Starting Text"
                    },
                    Button {
                        text: "Button Text"
                        action: function (): Void {
                            Text {
                                textOrigin: TextOrigin.TOP
                                font: Font {
                                    size: 16
                                }
                                content: "New Scene Text"
                            }
                            System.out.println("Button Text");
                        }
                    }
                ]
            }
        ]
    }
}

Open in new window

mitchguyAsked:
Who is Participating?
 
warsqlConnect With a Mentor Commented:
I only know JavaFX in theory, but I believe you bind the display text to a variable, then just change the value when the button is clicked.  JavaFX manages keeping the display consistent with your variable because the value is bound.

var message = "Starting Text"
....

Text {
                        textOrigin: TextOrigin.TOP
                        fill: Color.web("ox131021")
                        font: Font.font("Sans Serif", 14)
                        content: bind message
                    },
Button {
                        text: "Button Text"
                        action: function (): Void {
                            message = "New scene text"
                        }
                    }
....
0
 
mitchguyAuthor Commented:
That worked perfectly. I need to try and stop thinking in Java/C++. I thought it was going to be something like
scene.content.myText = "new scene text"

Thanks
0
All Courses

From novice to tech pro — start learning today.