模仿CTRL +使用Javafx在ListView中单击多个选择 [英] Mimicking CTRL+Click Multiple Selection in ListView using Javafx

查看:287
本文介绍了模仿CTRL +使用Javafx在ListView中单击多个选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到在ListView中选择多个项目的不同方法。 GUI将在触摸屏显示器上运行,因此我将无法按CTRL + Click。通过研究各种过去的帖子,我已经能够通过将所有选定的项目保存在一个数组中然后循环遍历它来获得最终选择来实现多项选择。我的代码唯一的问题是,与CTRL +点击相比,选择顺利完成,因为每次选择新项目时,我的代码会导致类型闪烁。所以基本上listView清除所有选择,然后选择正确的选择。有没有办法让这种转变顺利进行?是否更容易模仿触摸以获得CTRL +点击效果?

I'm trying to find different ways of selecting multiple items in a ListView. The GUI will be running on a touch screen monitor, So I won't be able to CTRL+Click. From researching through various past posts, I have been able to implement Multiple Selection via keeping all the selected items in an Array and then looping through it to get the final selections. The only problem I have with my code is that compared to a CTRL +click , the selection is done smoothly, where as my code leads to a type flickering every time a new item is selected. So basically the listView clears all the selections and then selects the correct ones. Is there a way to make this transition go smoothly? Would it be easier to mimic a touch to have CTRL+click effect?

selectedList = new int[totalTypes];//total number of item properties

for(int x=0; x<selectedList.length;x++){//0 = not selected, 1 = selected
    selectedList[x]=0;
}
testView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

    testView.setOnMouseClicked(new EventHandler<Event>(){
        @Override
        public void handle(Event event){
                if(selectedList[testView.getSelectionModel().getSelectedIndex()]==0){
                    selectedList[testView.getSelectionModel().getSelectedIndex()]=1;
                }
                else{
                    selectedList[testView.getSelectionModel().getSelectedIndex()]=0;
                }

                for(int x=0; x<selectedList.length;x++){
                    if(selectedList[x]==1){
                        testView.getSelectionModel().select(x); 
                    }
                    else{
                        testView.getSelectionModel().clearSelection(x);;
                    }
                }


        }

    });


推荐答案

您可以在用户点击时更改选择 ListCell 您自己而不是使用标准事件处理:

You could handle changing the selection when a user clicks a ListCell yourself instead of using the standard event handling:

@Override
public void start(Stage primaryStage) {
    ListView<Integer> listView = new ListView<>();
    listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    listView.getItems().setAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    listView.addEventFilter(MouseEvent.MOUSE_PRESSED, evt -> {
        Node node = evt.getPickResult().getIntersectedNode();

        // go up from the target node until a list cell is found or it's clear
        // it was not a cell that was clicked
        while (node != null && node != listView && !(node instanceof ListCell)) {
            node = node.getParent();
        }

        // if is part of a cell or the cell,
        // handle event instead of using standard handling
        if (node instanceof ListCell) {
            // prevent further handling
            evt.consume();

            ListCell cell = (ListCell) node;
            ListView lv = cell.getListView();

            // focus the listview
            lv.requestFocus();

            if (!cell.isEmpty()) {
                // handle selection for non-empty cells
                int index = cell.getIndex();
                if (cell.isSelected()) {
                    lv.getSelectionModel().clearSelection(index);
                } else {
                    lv.getSelectionModel().select(index);
                }
            }
        }
    });

    Scene scene = new Scene(listView);

    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于模仿CTRL +使用Javafx在ListView中单击多个选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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