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 ?
views:
245answers:
1
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
2010-05-06 18:55:15
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
2010-05-06 19:05:31
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
2010-05-06 19:07:34
i just removed all mouse listeners and replaced them with what i wantthanks for the help :)
yurib
2010-05-06 19:27:47