Disable row selection in TableView

I have a read-only table view in JavaFX 8, and I don't want users to select rows.
They should still sort the columns and scroll, just don't select any rows.
How can I achieve this?

+6
source share
4 answers

After a while I found how to solve it, so I am posting it here for future users.
The solution is based on this answer: JavaFX8 - Remove selection of highlighted line

After adding the following lines to your css, the selected lines will look exactly like the unselected lines, to achieve the same effect that I wanted in the same place:

.table-row-cell:filled:selected { -fx-background: -fx-control-inner-background ; -fx-background-color: -fx-table-cell-border-color, -fx-background ; -fx-background-insets: 0, 0 0 1 0 ; -fx-table-cell-border-color: derive(-fx-color, 5%); } .table-row-cell:odd:filled:selected { -fx-background: -fx-control-inner-background-alt ; } 
+7
source

You can disable selection by setting selectionModel to null .

 table.setSelectionModel(null); 
+9
source

I just got into this problem. I think the best way to solve it is to provide a null implementation of TableViewSelectionModel.

Then you can just say tableView.setSelectionModel(new NullTableViewSelectionModel(tableView));

An example of a zero implementation below ...

 import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.control.TableColumn; import javafx.scene.control.TablePosition; import javafx.scene.control.TableView; public class NullTableViewSelectionModel extends TableView.TableViewSelectionModel { public NullTableViewSelectionModel(TableView tableView) { super(tableView); } @Override public ObservableList<TablePosition> getSelectedCells() { return FXCollections.emptyObservableList(); } @Override public void selectLeftCell() { } @Override public void selectRightCell() { } @Override public void selectAboveCell() { } @Override public void selectBelowCell() { } @Override public void clearSelection(int i, TableColumn tableColumn) { } @Override public void clearAndSelect(int i, TableColumn tableColumn) { } @Override public void select(int i, TableColumn tableColumn) { } @Override public boolean isSelected(int i, TableColumn tableColumn) { return false; } @Override public ObservableList<Integer> getSelectedIndices() { return FXCollections.emptyObservableList(); } @Override public ObservableList getSelectedItems() { return FXCollections.emptyObservableList(); } @Override public void selectIndices(int i, int... ints) { } @Override public void selectAll() { } @Override public void clearAndSelect(int i) { } @Override public void select(int i) { } @Override public void select(Object o) { } @Override public void clearSelection(int i) { } @Override public void clearSelection() { } @Override public boolean isSelected(int i) { return false; } @Override public boolean isEmpty() { return false; } @Override public void selectPrevious() { } @Override public void selectNext() { } @Override public void selectFirst() { } @Override public void selectLast() { } } 
+4
source

I found here another solution for the same problem, but for ListView. Template: listen to the selection change event, and then clear the selection. It also works for TableView. Code example:

  _tableView.getSelectionModel() .selectedIndexProperty() .addListener((observable, oldvalue, newValue) -> { Platform.runLater(() -> { _tableView.getSelectionModel().clearSelection(); }); }); 
+4
source

Source: https://habr.com/ru/post/979263/


All Articles