“没有为顶级元素指定控制器”以编程方式设置Controller时 [英] "No Controller specified for top level element" when programatically setting a Controller

查看:1497
本文介绍了“没有为顶级元素指定控制器”以编程方式设置Controller时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FXML文件,其中包含带有onMouseClicked属性的按钮。我没有在FXML中设置一个控制器,因为我有构造函数注入我想要提供给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..." />

我以编程方式设置Controller,我的控制器包含FXML中指定的方法。 Controller函数执行,一切正常。

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);

我的问题在于IntelliJ检查.fxml文件。每次出现函数引用时,它都会报告错误:(45,54)没有为顶级元素指定控制器。我正在查看IntelliJ的检查规则,并且在JavaFX部分中没有看到此规则。同样,程序构建并运行得很好,因此它不是真正的编译错误。我想禁用此错误通知。

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.

如何避免此错误?

推荐答案

甚至,我不喜欢这些通知。我以编程方式设置控制器。要禁用它,您需要将突出显示级别设置为none。
要执行此操作,请打开fxml文件,在右侧的最底部,您将看到一个hector图标。单击hector图标并通过拖动滑块将高亮级别设置为none。

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.

您需要重新启动IDE才能使更改生效。仅对该文件的突出显示将设置为none。在我们将突出显示级别再次设置为无之前,其他文件的突出显示行为不会发生任何变化。

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.

如果您无法找到hector图标,请使用以下链接。
https://www.jetbrains.com/help/idea/ status-bar.html

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

这篇关于“没有为顶级元素指定控制器”以编程方式设置Controller时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆