JavaFX使listView单元格无法选择

时间:2017-11-12 11:36:50

标签: listview javafx

我找不到让ListViews的单元格无法选择的方法。

我有一个带有自定义单元格的ListView,里面有一些按钮,我想要它,所以你不能选择单元格,但可以使用按钮。我会想象像listView.setSelectable;listView.setSelectionModel(SelectionMode.NONE);这样的东西,但是没有这样的东西!

1 个答案:

答案 0 :(得分:0)

即使没有现有的选择模型实现也不允许选择任何项目,您可以自己实现:

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