简体   繁体   English

从另一个场景切换启用/禁用 MenuItem javafx?

[英]Toggle enable/disable MenuItem javafx from anohter scene?

Seems like i never gets correct reference from FXML.好像我从来没有从 FXML 得到正确的参考。

FXML file: FXML 文件:

<MenuBar xmlns:fx="http://javafx.com/fxml/1" fx:id="rootMenu" xmlns="http://javafx.com/javafx/8"
     fx:controller="my.MenuController">
<Menu mnemonicParsing="false" text="Analysis">
    <MenuItem fx:id="test" mnemonicParsing="false" onAction="#startNewAnalyze" text="New analysis"/>

Then in Controller file:然后在控制器文件中:

@FXML 
private static MenuItem test;

@FXML
private void initialize() {
    Systen.out.println(test.getText());
}
public static void setDisable(boolean enable) {
    test.setDisable(enable);
}

This will give Nullpointer when calling the MenuController.setDisable(false);这将在调用MenuController.setDisable(false);时给出 Nullpointer MenuController.setDisable(false);

Any ideas?有任何想法吗?

You could make an MenuBar-Controller to access those items.您可以创建一个 MenuBar-Controller 来访问这些项目。 Give your items and id with fx:id .使用fx:id提供您的物品和fx:id In the Controller class you can use your variable easily by:在 Controller 类中,您可以通过以下方式轻松使用变量:

@FXML
private MenuItem 'yourID';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM