-1

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?

user3505901
  • 408
  • 1
  • 6
  • 19
  • You error is in different code or FXML than is shown in your question. Maybe, you have a statement `fx_btnHourUp = new Button()` or something like that somewhere. For further assistance you will need to supply a [mcve](http://stackoverflow.com/help/mcve) (please make it minimal and executable). – jewelsea Aug 04 '14 at 18:34
  • http://pastebin.com/2b7AJSyw - my main file http://pastebin.com/sLQy1wz7 - my fxml http://pastebin.com/qmQv5rt3 - my javafx controller – user3505901 Aug 04 '14 at 22:21
  • It's not a lot of code but sometimes this thing doesn't show it up right, I posted it to paste bin it's really not long so if you could take a look that'd be nice. – user3505901 Aug 04 '14 at 22:22

2 Answers2

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"/>
Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I think you're right, can you show me how to write it based on my program? I'm not really sure how to get add it in my particular program I've tried a few different ways each one has given me an error. – user3505901 Aug 04 '14 at 23:46
  • Thank you for showing me that, I just tried doing fx:controller="application.FX_TimerController" and it's giving me errors, do you know why? It doesn't really give a line for it either...Here it is on pastebin though if you have time. http://pastebin.com/khEbyDkP – user3505901 Aug 05 '14 at 00:14
  • I did this in my Main.Java class: BorderPane myPane = (BorderPane) FXMLLoader.load(getClass() .getResource("FX-Timer.fxml")); I tried this too: BorderPane myPane = (BorderPane) FXMLLoader.load(getClass() .getResource("application.FX-Timer.fxml")); But it doesn't seem to make a difference, adding your code to the FXML still causes an error :/ – user3505901 Aug 05 '14 at 00:17
  • Wow! Thank you! I didn't think that made a difference, I kinda thought FXML was basically just XML and my editor wasn't bringing anything up when I tried fx:id originally so I assumed it was wrong. – user3505901 Aug 05 '14 at 00:38
  • I suggest using an editor which is aware of FXML elements (for example [IntelliJ idea](http://www.jetbrains.com/idea/) or the [e(fx)clipse project](http://www.eclipse.org/efxclipse/index.html)). – jewelsea Aug 05 '14 at 00:40
2

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.

Community
  • 1
  • 1
Forager
  • 307
  • 2
  • 10