2016-03-22 20 views
0

JavaFXメニューに動的にエントリを追加しようとしています。私は、グラフを描くことができるプログラムを持っています、新しいグラフが描かれるたびに、私はすべてのグラフでObservableListにエントリを追加します。
私のcontrollerがリストを守り、それぞれの変更の際にJavaFX viewMenuを修正する必要があります。しかし、私はそうする際に問題が発生しています。
最初の追加では、期待通りに表示されます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 } 

出力は、私が見て期待していかを示しています。 ObservableListsize()も私が期待しているとおりです。

答えて

1

あなたは削除するオブジェクトを渡す必要があるremoveAll(E...)を使用しています。引数を渡さないので、何も削除されません。リストを消去するには、clear()を使用します。履歴リストのすべての項目が削除されます。

+0

引数として 'graphHandler.getGraphs()'を渡そうとしましたが、うまくいきませんでした。ソリューションありがとう:) –

関連する問題