javafx - make ListView not selectable via mouse

旧城冷巷雨未停 提交于 2019-12-08 17:34:25

问题


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:


You could also try:

listview.setMouseTransparent( true );
listView.setFocusTraversable( false );



回答2:


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



回答3:


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();
            }
        });



回答4:


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
          }
      });



回答5:


Maybe this Pointe you to the right direction? restrict focus1

If not maybe setting a custom FocusModel could help?




回答6:


          @Override
          protected void updateItem(T item, boolean empty) {
              super.updateItem(item, empty);
              updateSelected(false);
          }

Every time you click on a cell, the cell's updateItem will call, so you can deselect it at here. If you check the base class Cell.java. It use this method to prevent empty cell being selected.

    protected void updateItem(T item, boolean empty) {
        setItem(item);
        setEmpty(empty);
        if (empty && isSelected()) {
            updateSelected(false);
        }
    }



回答7:


Try in the code:

list.setFocusTraversable( false );

and in CSS

.list-cell:odd, .list-cell:even
{
    -fx-background-color: white;
    -fx-text-fill: black;
}


来源:https://stackoverflow.com/questions/20621752/javafx-make-listview-not-selectable-via-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!