I have a read-only TableView in JavaFX 8, and I don't want' the users to select rows.
They should still be able to sort the columns and scroll, just not to select any rows.
How can I achieve this?
The default is false. Determines whether user can select a row while the table view is in editing mode. The default is false. Determines whether users can select a more than one row while in editing mode. The default is false. When the user taps a row, the table view calls the delegate method tableView (_:didSelectRowAt:).
They should still be able to sort the columns and scroll, just not to select any rows. How can I achieve this? Show activity on this post. You can disable selection, by setting selectionModel to null.
If you’re using a UITableViewController to display a table view, you get the behavior by setting the clearsSelectionOnViewWillAppear property to true. Otherwise, you can clear the selection in your view controller’s viewWillAppear (_:) method:
When the user taps a row, the table view calls the delegate method tableView (_:didSelectRowAt:). At this point, your app performs the action, such as displaying the details of the selected hiking trail:
You can disable selection, by setting selectionModel
to null
.
table.setSelectionModel(null);
After a while I found how to solve it so posting it here for future users.
The solution is based on this answer:
JavaFX8 - Remove highlighting of selected row
After adding the following lines to your css, selected lines will look exactly as unselected lines, achieving the same effect 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 ;
}
I've just hit this issue myself. I think the best way to solve it is to provide a null implementation of TableViewSelectionModel.
Then you can simply say tableView.setSelectionModel(new NullTableViewSelectionModel(tableView));
A sample null implementation is 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() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With