user3088010
user3088010

Reputation: 27

Can't manipulate Label from JavaFX

I'm trying to change the text of a JavaFX software GUI label every time the user goes to that page. For that, I must be able to change the values of that label, such as parse it to a variable and do these stuff.

Problem is, since the label is all written in FXML, I don't know how to use it properly on Java. I tried:

@FXML public Label WordID;

in order to make the label (which has "WordID" as an ID) an object, but I've failed. When I do that, the program simply won't go to that frame anymore. What do I do?

Code:

public class Screen3Controller implements Initializable, ControlledScreen {

ScreensController myController;

  @Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void goToScreen1(ActionEvent event){
   myController.setScreen(ScreensFramework.screen1ID);
}

@FXML
private void goToScreen2(ActionEvent event){
   myController.setScreen(ScreensFramework.screen2ID);
}

}

FXML:

<Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
  <font>
    <Font name="Avenir Roman" size="40.0" />
  </font>
</Label>

Button Listener from Screen2:

@FXML
private void goToScreen3(ActionEvent event){
   myController.setScreen(ScreensFramework.screen3ID);
   WordID.setText("LOl");
}

Error message:

Glass detected outstanding Java exception at -[GlassViewDelegate sendJavaMouseEvent:]:src/com/sun/mat/ui/GlassViewDelegate.m:543 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: ... com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173) at java.security.AccessController.doPrivileged(Native Method) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1444) ... 40 more Caused by: java.lang.NullPointerException at screensframework.Screen3Controller.mostrarPalavra(Screen3Controller.java:89) ... 50 more

Upvotes: 2

Views: 2595

Answers (1)

Math
Math

Reputation: 3396

To work with the Label programmatically all you need to do is to declare a variable in the Controller file with the same id as it is declared in your .fxml file:

YourFXML.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.sample.Screen3Controller ">
    ...
    <Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
      <font>
        <Font name="Avenir Roman" size="40.0" />
      </font>
    </Label>
    ...
</AnchorPane>

com.sample.Screen3Controller must reference an existing .java file in the com.sample package.

Screen3Controller .java

import javafx.scene.control.Label;  //make sure you're importing the JavaFX Label

public class Screen3Controller implements Initializable, ControlledScreen {

    ScreensController myController;
    @FXML private Label WordID;

    ...

    WordID.setText("some text...");

    ...
}

Upvotes: 1

Related Questions