views:

245

answers:

1

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 ?

A: 

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.

Cristian
this almost works, but i want to use multiple selection and i don't want the item to be selected and cleared with every single click on the list. thats why i wanted to disable the single click selection to begin with instead of letting it happen and then clearing it.
yurib
JList has 2 default mouselisteners, i was thinking about removing one of them but since i dont know what each of them does im afraid it might affect other behavior besides the selection.
yurib
i just removed all mouse listeners and replaced them with what i wantthanks for the help :)
yurib