javafx - make ListView not selectable via mouse


Question

Is there an option in JavaFX to deactivate the possibility to select the items in a ListView via mouse?

I'd like to just display a ListView without any user interaction possible.

1
7
12/18/2013 7:08:04 AM

Accepted Answer

You could also try:

listview.setMouseTransparent( true );
listView.setFocusTraversable( false );
13
12/18/2013 7:05:35 AM

Setting the list to mouse transparent will also prevent cells with interactable custom list cells from accepting focus.

The ideal solution is to use a special selection model:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.MultipleSelectionModel;

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

    @Override
    public ObservableList<Integer> getSelectedIndices() {
        return FXCollections.emptyObservableList();
    }

    @Override
    public ObservableList<T> getSelectedItems() {
        return FXCollections.emptyObservableList();
    }

    @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 boolean isSelected(int index) {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return true;
    }

    @Override
    public void selectPrevious() {
    }

    @Override
    public void selectNext() {
    }
}

Then set the model in the list view:

listView.setSelectionModel(new NoSelectionModel<MyType>());

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon