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

14,639

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
14,639

Related videos on Youtube

Stealth Rabbi
Author by

Stealth Rabbi

Tea. Earl Gray. Hot. Welcome to Earth. I dabble in .NET, Java, Android, SQL Server, Oracle, Google Earth, and some other things.

Updated on July 13, 2022

Comments

  • Stealth Rabbi
    Stealth Rabbi almost 2 years

    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?

    • qwerty
      qwerty over 3 years
      I'm having the same issue. Is there a way to report this to JetBrains so that they can fix it in an update?
    • Stealth Rabbi
      Stealth Rabbi over 3 years
      sure, you can send a support email from the intellij website.
    • J-D3V
      J-D3V over 2 years
      Did you ever contact JetBrains? It's a a couple of weeks from being 2022, and this bug still persists. It's driving me crazy. The answers below are unacceptable IMO.
    • J-D3V
      J-D3V over 2 years
      It looks like you never accepted an answer, did you find anything that worked?
    • Stealth Rabbi
      Stealth Rabbi over 2 years
      @JΛY-ÐΞV I do not believe so. I never upvoted any of the answers.
  • Mert Serimer
    Mert Serimer over 6 years
    if you specificy that controlller, then when you try to reach textfields,buttons in your controller class you will get nullpointer exception.
  • Jean-François Côté
    Jean-François Côté about 6 years
    OP said: " I do not set a Controller up in the FXML because I have constructor injected data I want to provide to the Controller."
  • Tharkius
    Tharkius over 2 years
    What is a hector? Aside from a known first name ofc.
  • J-D3V
    J-D3V over 2 years
    Is there a link for that work around? And what do you mean "com.example"? Is that suppose to be replaced by the project's package?
  • The 5th column mouse
    The 5th column mouse over 2 years
    I mean "your.package.name.MyController". The possible integration example you can find here: dzone.com/articles/fxml-javafx-powered-cdi-jboss and here you will find the information to constructor inject: baeldung.com/java-ee-cdi
  • The 5th column mouse
    The 5th column mouse over 2 years
    @JΛY-ÐΞV I get the work around running locally on my environment. So if you need help let me know.
  • J-D3V
    J-D3V over 2 years
    Okay, I had to go back to an old project writing a JSON parser written in C++ & implemented in a Native Node.js module, so I am not working with JFX/OpenFX for at least a month, maybe two. I have to come back to the FX project though, as I have it sold to a client already, and perhaps you will still be around? If you are I would certainly love to get some clarity from you. I would like to try implementing the project using CDI. Anyhow, meanwhile I have to do somthing w/ the bounty so it's yours.
  • J-D3V
    J-D3V over 2 years
    I appreciate you taking the time to make these comments!
  • J-D3V
    J-D3V over 2 years
    BTW, what is the 5th column mouse?
  • The 5th column mouse
    The 5th column mouse over 2 years
    Thank you, but I would like to know if the solution works for you. Here is the minimal implementation: github.com/rusty85/javafx-cdi-demo
  • The 5th column mouse
    The 5th column mouse over 2 years
    5th column mouse is very old american cartoon but exact translated in russian it would mean underdog mouse)
  • J-D3V
    J-D3V over 2 years
    Oh okay, because I am an American, I actually know exactly what you're talking about. I loved all those old cartoons. I really liked Mighty Mouse, and Wiley Coyote. Anyway, good pick for a name.