Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable JList selection change event and invoke it only on double click?

As the title says... I'd like items in a JList to be "selected" only when they are double clicked. what would be the best way to achieve this kind of behavior ?

like image 767
yurib Avatar asked Feb 02 '26 09:02

yurib


1 Answers

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 that works... I can't test it here.

like image 119
Cristian Avatar answered Feb 04 '26 23:02

Cristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!