1
votes

I could not find a way to make ListViews's cells unselectable.

I have a ListView with custom cells with some buttons inside and I want to make it, so that you cannot select the cell, but can use the buttons. I would imagine something like listView.setSelectable; or listView.setSelectionModel(SelectionMode.NONE);, but there is no such thing!

1

1 Answers

1
votes

Even if there is no existing selection model implementation that does not allow for selecting any items, you can implement one yourself:

public class EmptySelectionModel<T> extends MultipleSelectionModel<T> {

    // since this list is empty and unmodifiable, it's safe to use it as
    // both index and item list
    private ObservableList emptyList = FXCollections.emptyObservableList();

    @Override
    public ObservableList<Integer> getSelectedIndices() {
        return emptyList;
    }

    @Override
    public ObservableList<T> getSelectedItems() {
        return emptyList;
    }

    // just do nothing instead of selecting/unselecting anything

    @Override
    public void selectIndices(int index, int... indices) {
    }

    @Override
    public void selectAll() {
    }

    @Override
    public void selectFirst() {
    }

    @Override
    public void selectLast() {
    }

    @Override
    public void clearAndSelect(int index) {
    }

    @Override
    public void select(int index) {
    }

    @Override
    public void select(T obj) {
    }

    @Override
    public void clearSelection(int index) {
    }

    @Override
    public void clearSelection() {
    }

    @Override
    public void selectPrevious() {
    }

    @Override
    public void selectNext() {
    }

    @Override
    public boolean isSelected(int index) {
        return false; // no items are selected
    }

    @Override
    public boolean isEmpty() {
        return true; // selection is always empty
    }

}
listView.setSelectionModel(new EmptySelectionModel<>());