拖动时禁用 TreeNode Selection

Disable TreeNode Selection while dragging

我有一个 Jtree 和一个处理触摸设备滚动的自定义 MouseListener/MouseMotionListener。

我现在的问题是,当我开始拖动时,当我按下鼠标时,当前节点会立即被选中。

问题: 怎么关闭鼠标拖动时的自动选择(点击不拖动时应该是选中的)。

我的 DragscrollListener 中的 clickedpressed 函数是空的。

注:

我还没有编写 DragScrollListener - 它可以找到 here

示例代码:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

public class Test extends JFrame{

    public Test(){

        JTree tree = new JTree();

        JScrollPane pane = new JScrollPane(tree);

        DragScrollListener ds = new DragScrollListener(tree);
        tree.addMouseListener(ds);
        tree.addMouseMotionListener(ds);

        getContentPane().add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        this.setSize(100,100);

    }

    public static void main(String[]arghs){
        new Test();
    }
}

要在拖动完成时控制所需的行为,请覆盖在 通过扩展 DragScrollListener。请参阅评论以进行说明:

public class Test extends JFrame{

    public Test(){

        JTree tree = new JTree();

        JScrollPane pane = new JScrollPane(tree);

        //To control the needed behavior when drag is finished, override 
        //mouse released in DragScrollListener by extending it
        MyDragScrollListener ds = new MyDragScrollListener(tree);

        tree.addMouseListener(ds);
        tree.addMouseMotionListener(ds);

        getContentPane().add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        this.setSize(100,100);
    }

    //Subclass DragScrollListener to override mouse released 
    public class MyDragScrollListener extends DragScrollListener{

        public MyDragScrollListener(Component c) {
            super(c);
        }

        @Override
        public void mouseReleased(MouseEvent e){

            //add needed functionality when mouse is released 
            if( e.getSource() instanceof JTree) {

                System.out.println("source is a JTree");
                JTree tree = (JTree)e.getSource();

                //clear seelction
                 tree.clearSelection(); 

            }

            super.mouseReleased(e);
        }
    }

    public static void main(String[]arghs){
        new Test();
    }
}

如果代码不够清晰,请不要犹豫。