如何启用多重选择模式是ListView? [英] How do I enable multiple selection mode is ListView?

查看:133
本文介绍了如何启用多重选择模式是ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为ListView启用多种选择模式。但是,当我测试它时它似乎仍处于单选模式,我不确定出了什么问题。救命?谢谢。这是我的代码:

  ObservableList< String>字母
= FXCollections.observableArrayList(Aa,Bb,Cc,
Dd,Ee);

ListView< String> AlphabetsLv = new ListView< String>(字母);
AlphabetsLv.setPrefSize(80,80);
AlphabetsLv.getSelectionModel()。setSelectionMode(SelectionMode.MULTIPLE);


解决方案

选择时按住Shift或Ctrl键。 / p>

Shift - >选择范围,顶部选择到底部选择。

Ctrl - >添加单个选定的行。



如果您想在不使用密钥的情况下进行多项选择,可以使用此帖子中的单元格工厂:
在点击时取消选择javafx ListView上的项目



在你的情况下:

  AlphabetsLv.setCellFactory(alv  - > {
ListCell< String> cell = new ListCell<>();
cell.textProperty()。bind(cell.itemProperty());
cell.addEventFilter(MouseEvent.MOUSE_PRESSED,event - > {
AlphabetsLv.requestFocus();
if( !cell.isEmpty()){
int index = cell.getIndex();
if(AlphabetsLv.getSelection Model()。getSelectedIndices()。contains(index)){
AlphabetsLv.getSelectionModel()。clearSelection(index);
} else {
AlphabetsLv.getSelectionModel()。select(index);
}
event.consume();
}
});
返回单元格;
});


I tried to enable multiple selection mode to ListView. However, it still seems to be in single selection mode when I test it and I'm not sure what went wrong. Help? Thank you. Here is my code:

ObservableList<String> alphabets
            = FXCollections.observableArrayList("Aa", "Bb", "Cc",
                    "Dd", "Ee");

ListView<String> AlphabetsLv = new ListView<String>(alphabets);
AlphabetsLv.setPrefSize(80, 80);
AlphabetsLv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

解决方案

Press/hold down Shift or Ctrl while selecting.

Shift -> selects range, top selection to bottom selection.
Ctrl -> adds individual selected rows.

If you want multiple selection without using keys, you can use the cell factory from this post: Deselect an item on an javafx ListView on click

In your case:

    AlphabetsLv.setCellFactory(alv -> {
        ListCell<String> cell = new ListCell<>();
        cell.textProperty().bind(cell.itemProperty());
        cell.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
            AlphabetsLv.requestFocus();
            if (!cell.isEmpty()) {
                int index = cell.getIndex();
                if (AlphabetsLv.getSelectionModel().getSelectedIndices().contains(index)) {
                    AlphabetsLv.getSelectionModel().clearSelection(index);
                } else {
                    AlphabetsLv.getSelectionModel().select(index);
                }
                event.consume();
            }
        });
        return cell;
    });

这篇关于如何启用多重选择模式是ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆