下面列出了javafx.scene.control.TableView.TableViewSelectionModel#select ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("unchecked")
public void selectCells(TableView<?> tableView, String value) {
@SuppressWarnings("rawtypes")
TableViewSelectionModel selectionModel = tableView.getSelectionModel();
selectionModel.clearSelection();
JSONObject cells = new JSONObject(value);
JSONArray object = (JSONArray) cells.get("cells");
for (int i = 0; i < object.length(); i++) {
JSONArray jsonArray = object.getJSONArray(i);
int rowIndex = Integer.parseInt(jsonArray.getString(0));
int columnIndex = getColumnIndex(jsonArray.getString(1));
@SuppressWarnings("rawtypes")
TableColumn column = tableView.getColumns().get(columnIndex);
if (getVisibleCellAt(tableView, rowIndex, columnIndex) == null) {
tableView.scrollTo(rowIndex);
tableView.scrollToColumn(column);
}
selectionModel.select(rowIndex, column);
}
}
/** Set selection
* @param sel_row_col Flattened list of row, col, row, col, .. cell indices. May be <code>null</code>
*/
public void setSelection(final List<Integer> sel_row_col)
{
final TableViewSelectionModel<List<ObservableCellValue>> selection = table.getSelectionModel();
selection.clearSelection();
if (sel_row_col == null)
return;
final ObservableList<TableColumn<List<ObservableCellValue>, ?>> columns = table.getColumns();
int i = 0;
while (i < sel_row_col.size())
{
final int row = sel_row_col.get(i++);
final int col = sel_row_col.get(i++);
if (row < data.size() && col < columns.size())
selection.select(row, columns.get(col));
}
}
@Override
public boolean marathon_select(String value) {
TableView<?> tableView = (TableView<?>) node;
TableViewSelectionModel<?> selectionModel = tableView.getSelectionModel();
if ("".equals(value)) {
selectionModel.clearSelection();
return true;
} else if (value.equals("all")) {
int rowSize = tableView.getItems().size();
for (int i = 0; i < rowSize; i++) {
selectionModel.select(i);
}
return true;
} else if (selectionModel.isCellSelectionEnabled()) {
selectCells(tableView, value);
return true;
} else {
int[] selectedRows = getSelectedRows(value);
selectionModel.clearSelection();
for (int rowIndex : selectedRows) {
if (getVisibleCellAt(tableView, rowIndex, tableView.getColumns().size() - 1) == null) {
tableView.scrollTo(rowIndex);
}
selectionModel.select(rowIndex);
}
return true;
}
}