下面列出了javafx.scene.control.TableView#getSelectionModel ( ) 实例代码,或者点击链接到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);
}
}
@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;
}
}
public String getSelection(TableView<?> tableView) {
TableViewSelectionModel<?> selectionModel = tableView.getSelectionModel();
if (!selectionModel.isCellSelectionEnabled()) {
ObservableList<Integer> selectedIndices = selectionModel.getSelectedIndices();
if (tableView.getItems().size() == selectedIndices.size()) {
return "all";
}
if (selectedIndices.size() == 0) {
return "";
}
return getRowSelectionText(selectedIndices);
}
@SuppressWarnings("rawtypes")
ObservableList<TablePosition> selectedCells = selectionModel.getSelectedCells();
int[] rows = new int[selectedCells.size()];
int[] columns = new int[selectedCells.size()];
int rowCount = tableView.getItems().size();
int columnCount = tableView.getColumns().size();
if (selectedCells.size() == rowCount * columnCount) {
return "all";
}
if (selectedCells.size() == 0) {
return "";
}
JSONObject cells = new JSONObject();
JSONArray value = new JSONArray();
for (int i = 0; i < selectedCells.size(); i++) {
TablePosition<?, ?> cell = selectedCells.get(i);
rows[i] = cell.getRow();
columns[i] = cell.getColumn();
List<String> cellValue = new ArrayList<>();
cellValue.add(cell.getRow() + "");
cellValue.add(getColumnName(tableView, cell.getColumn()));
value.put(cellValue);
}
cells.put("cells", value);
return cells.toString();
}
public LazyLoadingBehavior(TableView<T> tableView) {
this(tableView, tableView.itemsProperty(), tableView.getSelectionModel());
}