How to set button text in JavaFX?

2020-05-09 21:58发布

问题:

I'm trying to set the button text using this code:

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 words for buttons yo.
    fx_btnHourUp.setText("HELLO");
}

Here's my FXML:

<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>

But it won't work I don't really get why, does anyone else know how to set button text using JavaFX? >.>; I feel like JavaFX isn't as straightforward as Swing...but anyways, anyone know what I'm doing wrong? Also, does anyone know any resources to learn FXML I like FXML more than coding it in Java but there doesn't seem to be much on it, am I like the only guy in the world who prefers FXML over JavaFX GUI coding?

回答1:

Are you sure your elements are actually not null and that your assert statements are actually doing what they're supposed to? Click me for info on using assert statements. Make sure your variable declarations are left as @FXML private Button fx_btnHourUp; and that you never instantiate the button yourself via fx_btnHourUp = new Button();.

Are you using SceneBuilder 2.0 to create your fxmls? It's a program that allows you to design fxmls while avoiding many of these kinds of common errors. Download link here.



回答2:

Why Button Text Does Not Change in Your Program

a. You never associate your controller with your FXML.

Because a controller is never associated with your FXML, a controller is never instantiated and your controller initialization code which sets the button text is never run.

b. You set a CSS id on your items in FXML rather than an FXML fx:id.

When you don't set an fx:id for items in FXML, FXML will not inject references to Java objects for those items into your controller class.

These are very common errors when first starting to write FXML applications. If you wanted, you could write to Oracle at javasedocs_us@oracle.com with a link to this answer and ask Oracle to add an FXML troubleshooting section or common issues section to their documentation.

Associating Controllers with FXML

EITHER:

  1. Use an fx:controller attribute.

    In your sample, add an fx:controller attribute to your root node; e.g.

    <BorderPane fx:controller="application.FX_TimerController" ...
    

    OR

  2. Set a controller in an FXML loader.

    Refer to Passing Parameters JavaFX FXML, for info on how to do this.

Setting fx:id Values

Instead of:

<Button ... id="fx_btnHourUp"/>

Write:

<Button ... fx:id="fx_btnHourUp"/>