且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在tableview JavaFx的同一行中选择不同的单元格

更新时间:2023-02-05 14:50:42

如果您使用的是细胞选择行选择(换句话说,如果你已经调用 tableView.getSelectionMode()。setCellSelectionEnabled(true); ),那么你应该观察所选单元格的列表,而不是 selectedItem 属性。 selectedItem 属性仅指示所选行,因此只有在您选择新行时才会更改。

If you are using cell selection instead of row selection (in other words, if you have called tableView.getSelectionMode().setCellSelectionEnabled(true);), then you should observe the list of selected cells instead of the selectedItem property. The selectedItem property only indicates the row that is selected, so it only changes if you select a new row.

ObservableList<TablePosition> selectedCells = table.getSelectionModel().getSelectedCells() ;
selectedCells.addListener((ListChangeListener.Change<? extends TablePosition> change) -> {
    if (selectedCells.size() > 0) {
        TablePosition selectedCell = selectedCells.get(0);
        TableColumn column = selectedCell.getTableColumn();
        int rowIndex = selectedCell.getRow();
        Object data = column.getCellObservableValue(rowIndex).getValue();
    }
});