選択の際に、選択した行を記録しておく方法はどうですか?
別の行が選択されたときに、記録された選択行を選択状態にします。
コード例は、最低限の実装で、動作がエレガントではないのですが、Ctrlキー無しで複数選択はできています。
(追記)setOnMousePressed
にするとエレガントでないのが解消されたのでコード例を修正しました。詳しくは、履歴とコメント欄を参照してください。
あと解除のことも考えてません。
- コード例:選択行を記録して選択状態を自動で復元する
lang
1// javafx.application.Applicationの実装クラス(部分)
2
3// 選択行を記録するConcurrentなSet
4private Set<Integer> selectedIndices = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
5
6@Override
7public void initialize(URL url, ResourceBundle rb) {
8 TableViewSelectionModel<TableRecord> tableViewSelectionModel = tableView.getSelectionModel();
9 tableViewSelectionModel.setSelectionMode(SelectionMode.MULTIPLE);
10 tableView.setOnMousePressed(evt -> {
11 // その時選択した行のインデックスを取得
12 ObservableList<Integer> indices = tableViewSelectionModel.getSelectedIndices();
13 // 以前選択したインデックスを選択状態にする
14 for (Integer index : selectedIndices) {
15 tableViewSelectionModel.select(index);
16 }
17 // 以前選択したインデックスリストに今回の選択を追加
18 for (Integer index : indices) {
19 selectedIndices.add(index);
20 }
21 });
22 // 以下略
23}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/30 11:27
2016/08/31 04:05
2016/08/31 04:58
2016/08/31 16:05