简体   繁体   中英

Delete selected elements on the JList while ALT + click

I have a JList . I would like to delete already selected values whenever i press ALT + mouseclick the same time on the list. How to do that? I don't know which listener to use.

The problem is that when I have selected values and click on the list again the old selection is lost, new item is selected.

I did:

        list.addMouseListener(new MouseListener() {

        ...

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println(e);
                int[] selected = list.getSelectedIndices();     
                if(selected.length > 0 && (e.getModifiers() & InputEvent.ALT_MASK) == InputEvent.ALT_MASK ){
                    System.out.println("tu ");
                    for(int i: selected){
                        model.remove(i);
                    }
                }

            }
....
}

But the problem is that only one element is deleted. This one on which i alt click

The problem is that when I have selected values and click on the list again the old selection is lost, new item is selected.

Alt+mousePressed is used to change the selection. This is a common usage of the Alt key. Try playing with this key combination on any application (ie. Windows Explorer) to see how it works.

So a better implementation would probably be to use a different mouse+key combination. I'm not sure what the standards are but I would suggest that for maybe just the "Delete" key could be used delete the selected items. Of course you should popup a confirm dialog. For using the mouse I would use a popup menu that would display on a right click and the menu would contain a Delete action. Again a confirm dialog should display.

Remember any time you build an application the user should be able to use the Keyboard or the mouse to achieve an Action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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