In Java FX, how do you set the text programmatically?


Question

Okay, so I want to change the text in a button using JavaFX but I can't figure out how :/ this is my code:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("FXML Timer");
        BorderPane myPane = (BorderPane) FXMLLoader.load(getClass()
            .getResource("FX-Timer.fxml"));
        Scene myScene = new Scene(myPane);
        myScene.getStylesheets().add(
            Main.class.getResource("application.css")
                            .toExternalForm());
        primaryStage.setResizable(false);
        primaryStage.setScene(myScene);
        primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

Here's my FXML:

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity"
    minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0"
    prefWidth="368.0" xmlns="http://javafx.com/javafx/8"     xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Label alignment="CENTER" contentDisplay="CENTER" prefHeight="187.0"
            prefWidth="365.0" text="Label" BorderPane.alignment="CENTER">
            <font>
                <Font size="96.0" />
            </font>
        </Label>
    </center>
    <bottom>
        <GridPane BorderPane.alignment="CENTER">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0"
                    vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <Button mnemonicParsing="false" prefHeight="53.0" prefWidth="92.0"
                    text="Hup" textAlignment="CENTER" alignment="CENTER" id="fx_btnHourUp"/>
                <Button mnemonicParsing="false" prefHeight="37.0" prefWidth="92.0"
                text="Hdown" GridPane.columnIndex="1" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnHourDown" />
                <Button mnemonicParsing="false" prefHeight="44.0" prefWidth="92.0"
                text="Mup" GridPane.columnIndex="2" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnMinuteUp" />
                <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="122.0"
                text="Mdown" GridPane.columnIndex="3" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnMinuteDown" />
            </children>
        </GridPane>
    </bottom>
    <top>
        <GridPane BorderPane.alignment="CENTER">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES"
                minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0"
                    vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <Button alignment="CENTER" contentDisplay="CENTER"
                    mnemonicParsing="false" prefHeight="54.0" prefWidth="183.0" text="Start" textAlignment="CENTER" />
                <Button mnemonicParsing="false" prefHeight="41.0" prefWidth="198.0"
                text="Stop" GridPane.columnIndex="1" textAlignment="CENTER"
                alignment="CENTER" />
            </children>
        </GridPane>
    </top>
</BorderPane>

Here's my FXML where I'm trying to change the text in the buttons I made in my FXML.

package application;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class FX_TimerController implements Initializable {
    @FXML
    private Button fx_btnStart;
    @FXML
    private Button fx_btnStop;
    @FXML
    public Button fx_btnHourUp;
    @FXML
    private Button fx_btnHourDown;
    @FXML
    private Button fx_btnMinuteUp;
    @FXML
    private Button fx_btnMinuteDown;
    Timer timer;

    //variables
    private boolean timerRunning = false; // variable to check if the timer is running
    private int hours = 0;//never higher than 24, never lower than 0
    private int minutes = 0;//never goes above 59 or lower than 0


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        //checking to see whether these buttons were loaded from the FXML file
        assert fx_btnStart != null : "fx:id=\"fx_btnStart\" was not injected: Check your FXML file 'FX-Timer.fxml'.";
        assert fx_btnStop != null : "fx:id=\"fx_btnStop\" INJECTION ERROR";
        assert fx_btnHourUp != null : "fx:id=\"fx_btnHourUp\" INJECTION ERROR";
        assert fx_btnHourDown != null : "fx:id=\"fx_btnHourDown\" INJECTION ERROR";
        assert fx_btnMinuteUp != null : "fx:id=\"fx_btnMinuteUp\" INJECTION ERROR";
        assert fx_btnMinuteDown != null : "fx:id=\"fx_btnMinuteDown\" INJECTION ERROR";
        //ending of initialization of FXML elements 

        //set arrows for buttons yo.
        fx_btnHourUp = new Button("HELLO");
    }

    }

In this last class I'm trying to change the text in fx_btnHourUp to "HELLO" but it doesn't work I've tried a few different ways, can someone show me how it's done and maybe also give me a resource to learn this better?

1
-1
8/4/2014 11:16:00 PM

Accepted Answer

You can use the setText method

1
8/4/2014 11:18:52 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon