JavaFXメニューに動的にエントリを追加しようとしています。私は、グラフを描くことができるプログラムを持っています、新しいグラフが描かれるたびに、私はすべてのグラフでObservableList
にエントリを追加します。
私のcontroller
がリストを守り、それぞれの変更の際にJavaFX view
のMenu
を修正する必要があります。しかし、私はそうする際に問題が発生しています。
最初の追加では、期待通りに表示されますthe first entry
。
2番目の追加では、リストにはthe first entry + the first entry + the last entry
が含まれています。
3番目の追加では、first entry + the first entry + the second entry + the first entry + the second entry + the last entry
が表示されます。私はあなたがこの時点からパターンを推測できると思います。
このコードスニペットは、私のコントローラから取得されます:JavaFXのメニューに動的エントリを追加する
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
を空にし、それを補充することでしたが、動作するようには思えません。誰かがアイデアを持っているのですか?
{ [[email protected]] added at 0 }
{ [[email protected]] added at 1 }
{ [[email protected]] added at 2 }
出力は、私が見て期待していかを示しています。 ObservableList
のsize()
も私が期待しているとおりです。
引数として 'graphHandler.getGraphs()'を渡そうとしましたが、うまくいきませんでした。ソリューションありがとう:) –