與James_D的建議解決了這個問題:
public class ControllableTableSelectionModel<T> extends TableViewSelectionModel<T>{
private TableViewSelectionModel<T> originalModel;
private ControlsTableView controller;
public ControllableTableSelectionModel(TableViewSelectionModel<T> originalModel, ControlsTableView controller) {
super(originalModel.getTableView());
this.originalModel=originalModel;
this.controller=controller;
}
@Override
public boolean isSelected(int row, TableColumnBase<T, ?> column) {
return originalModel.isSelected(row, column);
}
@Override
public void selectLeftCell() {
if(controller.canSelect(this))
{
originalModel.selectLeftCell();
}
}
@Override
public void selectRightCell() {
if(controller.canSelect(this))
{
originalModel.selectRightCell();
}
}
@Override
public void selectAboveCell() {
if(controller.canSelect(this))
{
originalModel.selectAboveCell();
}
}
@Override
public void selectBelowCell() {
if(controller.canSelect(this))
{
originalModel.selectBelowCell();
}
}
@Override
public void selectRange(int minRow, TableColumnBase<T, ?> minColumn, int maxRow,
TableColumnBase<T, ?> maxColumn) {
if(controller.canSelect(this))
{
originalModel.selectRange(minRow, minColumn, maxRow, maxColumn);
}
}
@Override
protected int getItemCount() {
return originalModel.getTableView().getItems().size();
}
@Override
protected T getModelItem(int index) {
return originalModel.getTableView().getItems().get(index);
}
@Override
public void focus(int index) {
originalModel.getTableView().getFocusModel().focus(index);
}
@Override
public int getFocusedIndex() {
return originalModel.getTableView().getFocusModel().getFocusedIndex();
}
@Override
public ObservableList<TablePosition> getSelectedCells() {
return originalModel.getSelectedCells();
}
@Override
public boolean isSelected(int row, TableColumn<T, ?> column) {
return originalModel.isSelected(row, column);
}
@Override
public void select(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.select(row, column);
}
}
@Override
public void clearAndSelect(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.clearAndSelect(row, column);
}
}
@Override
public void clearSelection(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.clearSelection(row, column);
}
}
}
有公共API中沒有具體落實。你最好的選擇是定義一個包裝類繼承'TableSelectionModel'。傳入從表視圖中獲得的選擇模型的默認實例,並在需要選擇更改時將方法調用委託給它。有很多方法可以編寫,但如果以這種方式實現它們,每個方法都變得非常微不足道。 –