Starting off with JavaFX : Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text


I'm starting off with JavaFX.

The error occurs when I execute my program, before I attempted to do this, it worked fine and button clicks worked, but that was before it was my intention to make the button click change the text.

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

<AnchorPane xmlns="" xmlns:fx="" fx:controller="application.SceneController">
      <AnchorPane layoutX="-100.0" layoutY="-224.0" prefHeight="572.0" prefWidth="430.0">
            <Button layoutX="205.0" layoutY="253.0" mnemonicParsing="false" onAction="#handleActionButton1" text="Do you want swag?" />
            <Text fx:id="myLabel" layoutX="280.0" layoutY="339.0" strokeType="OUTSIDE" strokeWidth="0.0" text="My text will change!" />

That's the FXML, on the line, you can see the text is "My text will change!". On Eclipse's FXML text editor it gives me this error : "You can not assign 'Text' to the controller field 'Label'".

This is my SceneController class:

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SceneController{

    private Label myLabel;

    public void handleActionButton1(ActionEvent event){
        System.out.println("Hello World!");
        myLabel.setText("Hello World!");


As I mentioned above, before I had a label, it would successfully output "Hello World" on the console, but now it does that, but doesn't execute my Java application and it gives me a long error:


    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/ Source)
    at Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/ Source)
    at$ Source)
    at Method)
    at$null$141(Unknown Source)
    at$$Lambda$37/ Source)
    at Source)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at javafx.fxml.FXMLLoader.injectFields(Unknown Source)
9/22/2014 6:04:13 AM

Accepted Answer

Inside your FXML, you have defined a Text, where as in your Controller, you are trying to use it as Label.

The stackTrace (error) is shouting it on the top of its voice :

Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text

In your controller, you need to change the myLabel definition to

private Text myLabel;
9/22/2014 6:38:50 AM

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