How to disable JList selection change event and trigger it only with double click?

As the title says ... I would like the elements in the JList to be "selected" only by double-clicking. what would be the best way to achieve this behavior?

+3
source share
1 answer

You can try something like this:

JList list = new JList(dataModel);
...
MouseListener mouseListener = new MouseAdapter() 
{
    public void mouseClicked(MouseEvent e) 
    {
        if (e.getClickCount() == 2) // double click?
        {
            int posicion = list.locationToIndex(e.getPoint());
            list.setSelectedIndex(posicion);
        }
        else if (e.getClickCount() == 1) // single click?
            list.clearSelection() ;
    }
};
list.addMouseListener(mouseListener);

Tell me if this works ... I can't check it here.

+1
source

Source: https://habr.com/ru/post/1744386/


All Articles