2016-12-01 5 views
5

ListViewで複数の項目を選択するさまざまな方法を見つけようとしています。 GUIはタッチスクリーンモニター上で実行されるので、私はCTRL +クリックすることができません。さまざまな過去の記事を調べて、私は選択されたアイテムをすべてArrayに保持し、それをループして最終的な選択を得ることでMultiple Selectionを実装することができました。私のコードで唯一の問題は、CTRL +クリックと比較すると、選択がスムーズに行われていることです。コードが新しい項目が選択されるたびに点滅するタイプになります。したがって、基本的にlistViewはすべての選択をクリアし、正しいものを選択します。この移行を円滑に進める方法はありますか? CTRL +クリック効果を持つようにタッチを模倣する方が簡単でしょうか?Javafxを使用してCtrl +複数の選択をListViewでクリックします。

selectedList = new int[totalTypes];//total number of item properties 

for(int x=0; x<selectedList.length;x++){//0 = not selected, 1 = selected 
    selectedList[x]=0; 
} 
testView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 

    testView.setOnMouseClicked(new EventHandler<Event>(){ 
     @Override 
     public void handle(Event event){ 
       if(selectedList[testView.getSelectionModel().getSelectedIndex()]==0){ 
        selectedList[testView.getSelectionModel().getSelectedIndex()]=1; 
       } 
       else{ 
        selectedList[testView.getSelectionModel().getSelectedIndex()]=0; 
       } 

       for(int x=0; x<selectedList.length;x++){ 
        if(selectedList[x]==1){ 
         testView.getSelectionModel().select(x); 
        } 
        else{ 
         testView.getSelectionModel().clearSelection(x);; 
        } 
       } 


     } 

    }); 
+0

は、あなたが使用する必要があり、あなたがタッチ対応デバイス上でそれを実行したいようです'setOnMouseClicked'の代わりに' setOnTouchPressed' – TomN

+0

フラッシュは 'clear and reset'アクションによって引き起こされるはずですが、私はそれが正しいデザインのために必要ではないと思います。 – TomN

答えて

4

ユーザーがListCellをクリックしたときに代わりに標準イベント処理を使用しての自分で選択を変更扱うことができる:

@Override 
public void start(Stage primaryStage) { 
    ListView<Integer> listView = new ListView<>(); 
    listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 
    listView.getItems().setAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
    listView.addEventFilter(MouseEvent.MOUSE_PRESSED, evt -> { 
     Node node = evt.getPickResult().getIntersectedNode(); 

     // go up from the target node until a list cell is found or it's clear 
     // it was not a cell that was clicked 
     while (node != null && node != listView && !(node instanceof ListCell)) { 
      node = node.getParent(); 
     } 

     // if is part of a cell or the cell, 
     // handle event instead of using standard handling 
     if (node instanceof ListCell) { 
      // prevent further handling 
      evt.consume(); 

      ListCell cell = (ListCell) node; 
      ListView lv = cell.getListView(); 

      // focus the listview 
      lv.requestFocus(); 

      if (!cell.isEmpty()) { 
       // handle selection for non-empty cells 
       int index = cell.getIndex(); 
       if (cell.isSelected()) { 
        lv.getSelectionModel().clearSelection(index); 
       } else { 
        lv.getSelectionModel().select(index); 
       } 
      } 
     } 
    }); 

    Scene scene = new Scene(listView); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

これは完璧に動作します。ありがとうございました!好奇心のために、ここでノードはどのように機能しますか?私はそれを細胞に割り当てることに気付きます。私はタッチスクリーンモニターの使用を計画しているので、私の頭の中では、指のタッチがマウスクリックとして入力されると思いますか?または、私はちょうど押されたタッチにイベントを変更する必要がありますか?それから私はノードを得ることができません。 – Ammar

+0

@Ammar:['MouseEvent.isSynthesized'](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/MouseEvent.html#isSynthesized--)は、タッチイベントマウスイベントを引き起こします。私はこれをテストしなかった。答えのコードがどのように働くかは、親として 'ListCell'を見つけようとするために' ListView'の子孫Node(例えば 'ListCell'の' Text'ノードであるかもしれません)を使うことです。これを行うことができる場合、さらなるイベント処理が停止され、その「ListCell」からの情報が選択を変更するために使用される。それ以外の場合は、セルはクリックされず、イベントは通常どおりに処理されます... – fabian

+0

ああ私は、今より多くの意味を理解しています。私はあなたの助けに感謝します! – Ammar

関連する問題