"Controller not specified for top-level item" when programmatically configuring the controller

I have an FXML file with buttons with onMouseClicked attributes. I do not install the controller in FXML because I have a constructor that enters the data that I want to provide to the controller.

<Button mnemonicParsing="false" onMouseClicked="#newWidgetRequestButtonClicked" text="New Widget..." /> 

I install the controller programmatically, and my controller contains the methods specified in FXML. The controller functions are executed, and everything works fine.

 FXMLLoader loader = new FXMLLoader(getClass().getResource("MainView.fxml")); MyController controller = new MyController(...); loader.setController(controller); 

My problem is checking IntelliJ .fxml files. In each case, he refers to a function reference as saying "Error: (45, 54) The controller is not specified for the top-level element." I review IntelliJ inspection rules and cannot see this rule in the JavaFX section. Again, the program builds and works very well, so it is not a real compilation error. I want to disable this error notification.

How to avoid this error?

+7
java intellij-idea javafx
source share
2 answers

Even, I do not like these notifications. I programmed controllers. To disable it, you need to set the backlight level to none. To do this, open the fxml file and at the very bottom on the right side, you will see a hectare icon. Click on the iterator icon and set the backlight level to none by dragging the slider.

You will need to restart the IDE for the change to take effect. The highlight will only be set for this file. Other files will not see changes in their highlight style until we set the backlight level again.

Use the following link if you were unable to find the hector icon. https://www.jetbrains.com/help/idea/status-bar.html

+2
source share

You need to add the top level element fx: controller.

Suppose you have a basic fxml file with a right anchor panel with a button like fxml below.

 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Button layoutX="102.0" layoutY="50.0" mnemonicParsing="false" text="Button" /> </children> </AnchorPane> 

In this case, your top-level element will be an anchor panel. If you want to use action buttons such as onMouseClicked , you need to specify fxml your controller class in the top-level element (in this case, the binding kernel), as shown below.

 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.Controller"> <children> <Button fx:id="buttonExample" layoutX="102.0" layoutY="50.0" mnemonicParsing="false" text="Button" /> </children> </AnchorPane> 

fx:controller="com.example.Controller" indicates that my control class is the Controller that is in the com.example package.

And your item id should start with fx , as in the example ( fx:id="buttonExample" ).

+1
source share

All Articles