java - "No Controller specified for top level element" when programatically setting a Controller -
i have fxml file has buttons onmouseclicked attributes. not set controller in fxml because have constructor injected data want provide controller.
<button mnemonicparsing="false" onmouseclicked="#newwidgetrequestbuttonclicked" text="new widget..." />
i'm setting controller programatically, , controller contains methods specified in fxml. controller functions execute, , 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 .fxml files. on each occurrence of function reference, reports "error:(45, 54) no controller specified top level element". looking @ intellij's inspection rules , not see rule in javafx section. again, program builds , runs fine, not real compilation error. want disable error notification.
how can avoid error?
you need add top level element fx:controller.
lets have basic fxml file anchor pane button 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 anchor pane in case. if want use action buttons onmouseclicked
need tell fxml controller class in top level element (in case anchor pane) 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 control class controller in com.example package.
also elements id's should begin fx in example (fx:id="buttonexample"
).
Comments
Post a Comment