Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable row selection in TableView

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?

like image 274
BioRoy Avatar asked Dec 08 '14 08:12

BioRoy


People also ask

What is the default setting of Tableview didselectrowat?

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:).

Is it possible to disable selection in selectionmodel?

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.

How do I clear the selection in a uitableviewcontroller?

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:

What happens when a user taps a row in a Tableview?

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:


3 Answers

You can disable selection, by setting selectionModel to null.

table.setSelectionModel(null);
like image 56
user3224416 Avatar answered Oct 08 '22 10:10

user3224416


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 ;
}
like image 11
BioRoy Avatar answered Oct 08 '22 10:10

BioRoy


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() {

    }
}
like image 6
Phil Anderson Avatar answered Oct 08 '22 09:10

Phil Anderson