简体   繁体   English

JList删除按键上的选定项目,然后单击

[英]JList remove selected items on keystroke and click

My task is to enable removing Jlist selected elements when alt is pressed and jlist is clicked. 我的任务是在按alt并单击jlist时,启用删除Jlist选定元素的功能。 I did this by adding mouse listener to my jlist: 我是通过将鼠标侦听器添加到我的jlist来实现的:

list.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e) {
            java.util.List selectedItems = list.getSelectedValuesList();
            if (e.isAltDown()){
                for (Object o : selectedItems){
                    cm.removeElement(o); //cm is my custom model
                }
            }
        }
    });

My issue is that when there are two elements selected and I click the list with alt pressed only nearest element gets selected and is removed then. 我的问题是,当选择了两个元素并且单击alt的同时单击列表时,只有最近的元素被选中并随后被删除。 I have no clue how to remove several elements with this input combination. 我不知道如何使用此输入组合删除多个元素。

The problem is that the mouse click clears all the previous selections and then selects the row you just clicked on. 问题在于,单击鼠标会清除所有先前的选择,然后选择您刚刚单击的行。 So therefore only that row is deleted. 因此,仅删除该行。

So instead you should be handling a "right mouse" click and then use the right mouse button only for the deletion of the item. 因此,您应该处理“鼠标右键”单击,然后仅将鼠标右键用于项目的删除。

if (e.isAltDown() && SwingUtilities.isRightMouseButton(e)) {

Or if you really want to do this on a left mouse click then you would probably need to use a ListSelectionListener . 或者,如果您真的想用鼠标左键执行此操作,则可能需要使用ListSelectionListener Every time the selection changes you would need to use the getSelectedValuesList() method and save the List returned from the method. 每次更改选择时,您都需要使用getSelectedValuesList()方法并保存从该方法返回的List Then in the MouseListener you would access the saved List instead of getting the currently selected List of items. 然后,在MouseListener您将访问保存的列表,而不是获取当前选择的项目列表。

I don't like this approach because the logic is now contained in two separate listeners. 我不喜欢这种方法,因为逻辑现在包含在两个单独的侦听器中。 Although I guess you could create a class that implement both the selection listener and the mouse listener. 尽管我猜您可以创建一个同时实现选择侦听器和鼠标侦听器的类。

This is not a perfect answer. 这不是一个完美的答案。 But it solves the issue . 但这解决了这个问题

I just tried to see how the selection event works. 我只是想看看选择事件是如何工作的。 When he selection happens an Mouse pressed event is triggered and then the Selection happens. 当他的选择发生时,将触发鼠标按下事件,然后选择发生。 So the MouseListeners which are already added to the component are responsible to make the selection. 因此,已经添加到组件中的MouseListener负责进行选择。 Removing the MouseListeners which are already in place would prevent the selection happen using mouse. 删除已经存在的MouseListener将防止使用鼠标进行选择。 So i did this. 所以我做到了。

        MouseListener[] adapters = list.getMouseListeners();
        for (int i = 0; i < adapters.length; i++) {
            list.removeMouseListener(adapters[i]);
        }

Now user will not be able to do the selection using mouse but he will be make the selection using keyboard. 现在,用户将无法使用鼠标进行选择,但可以使用键盘进行选择。 So the below would work. 所以下面的工作。

       list.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
            java.util.List selectedItems = list.getSelectedValuesList();
            if (e.isAltDown()){
                for (Object o : selectedItems){
                    model.removeElement(o); //cm is my custom model
                }
            }
            }
        });

I think the answer given by camickr , should be followed. 我认为应该遵循camickr给出的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM