在JavaFX中向菜单添加动态条目 [英] Adding dynamic entries to Menu in JavaFX

查看:573
本文介绍了在JavaFX中向菜单添加动态条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将条目动态添加到JavaFX菜单中.我有一个可以绘制图形的程序,每次绘制新图形时,我都会向所有图形的ObservableList中添加一个条目.
我的controller会观察列表,并在每次更改时都应修改JavaFX view中的Menu.但是,这样做时我遇到了问题.
在第一个添加项上,它按预期显示了the first entry.
在第二个添加项中,列表包含了the first entry + the first entry + the last entry.
在第三个添加项中它显示first entry + the first entry + the second entry + the first entry + the second entry + the last entry.我想您可以从现在开始猜测模式.
此代码段摘自我的控制器:

I'm trying to dynamically add entries to a JavaFX menu. I have a program that can draw graphs, each time a new graph is drawn, I add an entry to the an ObservableList with all graphs.
My controller observes the list and on each change, it should modify the Menu in the JavaFX view. However I'm running in a problem when doing so.
On the first add, it shows as expected the first entry.
On the second add, the list contains the first entry + the first entry + the last entry.
On the third add it shows the first entry + the first entry + the second entry + the first entry + the second entry + the last entry. I guess you can guess the pattern from this point forward.
This code snippet is taken from my controller:

graphHandler.getGraphs().addListener(new ListChangeListener<Graph>() {
//GraphHandler.class is my model, that holds the list with all graphs
    @Override
    public void onChanged(Change<? extends Graph> c) {
        Menu history = graphView.getHistoryMenu();
        history.getItems().removeAll();
        //on change get the Menu from the model and empty it
        String changes = (String) c.toString();
        System.out.println(changes);
        //Output will be added below snippet
        graphHandler.getGraphs().forEach((Graph graph) -> {
            String root = graph.getRootNode().toString();
            MenuItem item = new MenuItem(root);
            //Get the graph's root node name and add a MenuItem with it's name
            item.setOnAction(new EventHandler<ActionEvent>() {
            //On choosing a MenuItem load the according graph; works fine
                @Override
                public void handle(ActionEvent event) {
                    graphHandler.getGraphByRootNode(root);
                }
            });
            history.getItems().addAll(item);
            //Add all MenuItems to the Menu
        });
    }
});

我的方法是在每次更改时清空Menu并重新填充它,但这似乎不起作用.有人知道我缺少什么吗?

My approach was to empty the Menu on every change and refill it, but it doesn't seem to work. Does anybody have an Idea what I'm missing?

{ [com.example.d3project.model.Graph@7eb5106] added at 0 }
{ [com.example.d3project.model.Graph@f510ff2] added at 1 }
{ [com.example.d3project.model.Graph@69239a8d] added at 2 }

输出显示了我期望看到的内容. ObservableListsize()也是我所期望的.

The output shows what I'm expecting to see. Also the size() of the ObservableList is as I'm expecting it.

推荐答案

您正在使用

You are using removeAll(E...) where you need to pass the objects you want to remove. Since you don't pass any argument nothing will be removed. To clear the list use clear() which will remove all items in your history list.

这篇关于在JavaFX中向菜单添加动态条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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