Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx - make ListView not selectable via mouse

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.

like image 717
user3038646 Avatar asked Dec 16 '13 21:12

user3038646


4 Answers

You could also try:

listview.setMouseTransparent( true );
listView.setFocusTraversable( false );
like image 85
Jurgen Avatar answered Nov 17 '22 08:11

Jurgen


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>());
like image 22
Mordechai Avatar answered Nov 17 '22 09:11

Mordechai


You may block your list view mouse click buy using event handler

listview.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                System.out.println(">> Mouse Clicked");
                event.consume();
            }
        });
like image 21
Shreyas Dave Avatar answered Nov 17 '22 08:11

Shreyas Dave


Mordechai's approach works really great.
I tried to comment with another addition for JFoenix JFXListView but the code formatting wasn't good so I created this answer:

If you are using JFoenix and the JFXListView you have also the behaviour that you can still see the JFXRippler on cell selection.
To prevent this or change the rippler override the cell factory:

//Replace T with your specific type.
listView.setCellFactory(param -> new JFXListCell<T>() {

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        cellRippler = new JFXRippler(); //create empty rippler
    }
});
like image 24
schlegel11 Avatar answered Nov 17 '22 08:11

schlegel11