我正試圖編寫自定義TableViewSelectionModel
以便在JavaFX中使用我的TableView
。我已經實現了所有的抽象方法,並且正在更新所選項目和所選單元格列表。但是,當我單擊另一行時,表中顯示的實際選擇不會改變。自定義TableViewSelectionModel沒有更新
下面是一個簡單的例子,它顯示了這種行爲:
import java.util.*;
import javafx.scene.control.*;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.collections.*;
import javafx.beans.property.ReadOnlyListWrapper;
public class MySelectionModel<S> extends TableViewSelectionModel<S>{
Set<Integer> selection = new HashSet<>();
final ObservableList<TablePosition<S, ?>> selectedCells = FXCollections.<TablePosition<S, ?>>observableArrayList();
final ObservableList<S> selectedItems = FXCollections.<S>observableArrayList();
final ObservableList<Integer> selectedIndices = FXCollections.<Integer>observableArrayList();
public MySelectionModel(TableView<S> tableview){
super(tableview);
setCellSelectionEnabled(false);
}
public ObservableList<S> getSelectedItems(){
return new ReadOnlyListWrapper<S>(selectedItems);
}
public void clearSelection(int row, TableColumn<S, ?> tableColumn){
selection.remove(Integer.valueOf(row));
updateSelection();
}
public void clearAndSelect(int row, TableColumn<S, ?> tableColumn){
selection = Collections.singleton(row);
updateSelection();
}
public void select(int row, TableColumn<S, ?> tableColumn){
selection.add(Integer.valueOf(row));
updateSelection();
}
public boolean isSelected(int row, TableColumn<S, ?> tableColumn){
return selection.contains(Integer.valueOf(row));
}
public ObservableList<TablePosition> getSelectedCells(){
return new ReadOnlyListWrapper<TablePosition>((ObservableList<TablePosition>)(Object)selectedCells);
}
public ObservableList<Integer> getSelectedIndices(){
return new ReadOnlyListWrapper<Integer>(selectedIndices);
}
public void selectBelowCell(){}
public void selectAboveCell(){}
public void selectRightCell(){}
public void selectLeftCell(){}
public void updateSelection(){
List<TablePosition<S, ?>> positions = new ArrayList<>();
List<S> items = new ArrayList<>();
List<Integer> indices = new ArrayList<>();
TableView<S> tableView = getTableView();
for(Integer i : selection){
positions.add(new TablePosition<S, Object>(tableView, i.intValue(), null));
items.add(getTableView().getItems().get(i.intValue()));
indices.add(i);
}
selectedCells.setAll(positions);
selectedItems.setAll(items);
selectedIndices.setAll(indices);
}
}
正在被在TableView
這樣設置:
TableView<ObservableList<MyData>> table = new TableView<>();
table.setSelectionModel(new MySelectionModel(table));
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
我已經證實,新指數是正確的通過在兩個支持ObservableList
s上添加ListChangeListener
s並顯示Change
s來設置。輸出是我期望的,例如
選定的單元格{[TablePosition [行:8,柱:空,的tableView:TableView中@ 11924c25 [的styleClass =表視圖]]]由[TablePosition [行替換:12,柱:空,的tableView:TableView中@ 11924c25 [的styleClass =表視圖]]]在0}
所選項目{[[MyCustomTableView $細胞@ 1c988ebb,MyCustomTableView $細胞@ 6b80b2be]]由[[MyCustomTableView $細胞@ 4a17d76d,MyCustomTableView $細胞替換@ 6e48d6d] at 0}
是否有任何我丟失的,這將需要TableView
更新其選擇時, TableViewSelectionModel
更改選擇?
您是在執行'getSelectedIndices()',還是在選擇更改時修改該列表? –
@James_D我沒去過,但我已經加入了,它仍然顯示同樣的問題。 – resueman
可能需要[MCVE]。 (我知道用'TableViewSelectionModel'實現很難做到最小化。) –