如何禁用JList选择更改事件并只在双击时调用它?

时间:2021-06-15 20:02:30

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 ?

就像标题说的……我希望JList中的项目只有在双击时才被“选中”。达到这种行为的最佳方式是什么?

1 个解决方案

#1


1  

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.

告诉我这样行吗?我不能在这里测试。

#1


1  

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.

告诉我这样行吗?我不能在这里测试。