Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No Controller specified for top level element" when programatically setting a Controller

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?

like image 992
Stealth Rabbi Avatar asked Sep 30 '15 14:09

Stealth Rabbi


3 Answers

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").

like image 105
emin deniz Avatar answered Oct 20 '22 03:10

emin deniz


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

like image 26
lambad Avatar answered Oct 20 '22 05:10

lambad


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

like image 2
The 5th column mouse Avatar answered Oct 20 '22 05:10

The 5th column mouse