MenuButton不显示Javafx中的项目 [英] MenuButton doesn't display items in Javafx

查看:131
本文介绍了MenuButton不显示Javafx中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Foo,只需加载FXML并创建场景。

I have a class Foo which just load the FXML and create the scene.

在FXML中,我将控制器设置为FooController( fx:controller =FooController

In the FXML, I set the controller to be FooController (fx:controller="FooController")

我添加一个MenuButton:

And I add a MenuButton:

<MenuButton fx:id="menuButton" layoutX="264.1875" layoutY="146.5" mnemonicParsing="false" text="MenuButton" />

我尝试在FooController中设置menuButton:

And I try to set the menuButton in the FooController:

public class FooController implements Initializable{
    @FXML
    final  MenuButton menuButton = new MenuButton("Modalities");
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        final ObservableList<CheckMenuItem> listFilter = FXCollections.observableArrayList();
        final  MenuButton menuButton = new MenuButton("Modalities");
        CheckMenuItem item1 = new CheckMenuItem("T1");
        CheckMenuItem item2 = new CheckMenuItem("T1C");
        CheckMenuItem item3 = new CheckMenuItem("T2");
        listFilter.addAll(item, item2, item3);
        menuButton.getItems().addAll(listFilter);
        menuButton.setId("menuButton");
    }
}

但是尽管设置了MenuButton的所有功能,它还没有t显示GUI中的任何CheckMenuItems。

But despite of setting everything for the MenuButton it doesn't display any of the CheckMenuItems in the GUI.

如何在FXML中定义的menuButton中加载这些项目?

推荐答案

从不 @FXML 初始值设置为新值。

Never set an @FXML initialized value to a new value.

在您发布的代码中,您正在执行此操作两次,而您根本不应该这样做。

In your posted code, you are doing this twice, when you should not be doing it at all.

FXMLLoader将在它实例化的组件的层次结构中创建新项目,并将对这些新项目的引用注入到控制器中。如果您随后将这些引用设置为新值,则新值将永远不会包含在显示的组件层次结构中,除非您在那里专门添加它们,这首先忽略了使用FXML的目的,因为它忽略了FXML中定义的内容文件。

The FXMLLoader will create new items within the hierarchy of the component it instantiates and inject references to these new items into your controller. If you then set these references to new values, the new values will never be included in the displayed component hierarchy unless you specifically add them there, which kind of defeats the purpose of using FXML in the first place as it ignores things defined in your FXML file.

你应该拥有的是:

public class FooController {
    @FXML
    MenuButton menuButton;

    public void initialize() {
        menuButton.getItems().addAll(
            FXCollections.observableArrayList(
                new CheckMenuItem("T1"),
                new CheckMenuItem("T1C"),
                new CheckMenuItem("T2")
            )
        );
    }
}

另请注意,如果上面代码中的CheckMenuItems是静态而不是动态列表,那么您可以在FXML文档中定义它们,而不是在代码中创建它们。

Also note that if your CheckMenuItems in the above code are static rather than a dynamic list, then you could just define them all in your FXML document instead of creating them in code.

注意:这个是一个重复的问题,之前已被问过(不是确切的问题,但它的实质),但我的谷歌技能无法挖出一些重复。

Note: This is a duplicate question and has been asked before (not the exact question, but the substance of it), but my google skills couldn't dig up some of the duplicates.

这篇关于MenuButton不显示Javafx中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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