My JavaFX application has a Label with an fx:id of location
. It is defined in an FXML file. When I try to run the application, I get the following error:
java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field sample.Controller.location to java.net.URL
I am using JDK 12 with JavaFX 11.0.2.
I've seen other answers here on SO that say this is caused by a conflicting type for the location
Label. For example, it might be declared as a Label in the FXML file but in the Java code it is something else (in this case, java.net.URL). However, as you can see in the code below, I am not using the URL class anywhere.
Changing the fx:id to something else (such as loc
) makes the error go away, so location
must be a "magic" name.
What is causing this?
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label fx:id="location" layoutX="133.0" layoutY="146.0" text="Output" />
</children>
</Pane>
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 275));
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller
{
@FXML
Label location;
}
module-info.java
module MyTest
{
requires javafx.controls;
requires javafx.fxml;
opens sample;
}