I have an FXML file that has buttons with onMouseClicked attributes. I do not set a Controller up in the FXML because I have constructor injected data I want to provide to the Controller.
<Button mnemonicParsing="false" onMouseClicked="#newWidgetRequestButtonClicked" text="New Widget..." />
I'm setting the Controller programatically, and my controller contains the methods specified in the FXML. The Controller functions execute, and everything works fine.
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainView.fxml"));
MyController controller = new MyController(...);
loader.setController(controller);
My problem lies in IntelliJ's inspection of the .fxml files. On each occurrence of the function reference, it reports "Error:(45, 54) No controller specified for top level element". I am looking at IntelliJ's inspection rules and do not see this rule in the JavaFX section. Again, the program builds and runs just fine, so it is not a real compilation error. I want to disable this error notification.
How can I avoid this error?
You need to add top level element fx:controller.
Lets say you have a basic fxml file with a just anchor pane with a button like the 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>
Your top level element will be the anchor pane in this case. If you want to use action buttons like onMouseClicked
you need to tell to fxml you controller class in your top level element (in this case anchor pane) like 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"
line says that my control class is Controller which is in the com.example package.
Also your elements id's should begin with fx like in the example (fx:id="buttonExample"
).
Even, I don't like those notifications. I set the controllers programatically. To disable it, you need to set the highlighting level to none. To do this, open your fxml file and at the very bottom on the right side, you will see a hector icon. Click on the hector icon and set the highlight level to none by dragging the slider.
You will need to restart the IDE for the changes to take effect. The highlighting will be set to none for that file only. Other files won't see any change in their highlighting behavior until we set their highlighting level to none again.
Use the following link, if you were unable to find hector icon. https://www.jetbrains.com/help/idea/status-bar.html
It is nice task to find out the work-around for this issue. The IntelliJ is able to follow the reference via attribute fx:controller="your.package.here.MyController"
.
The defined parameters should be passed the controller new MyController(...);
.
This is the specified task.
So, how about integrate JavaFX and CDI? Instead of annotating the attributes, you can add the @Inject
annotation on a constructor.
In this way you can create an instance of the controller with specified parameters and set the reference using the fx:controller
attribute.
https://github.com/rusty85/javafx-cdi-demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With