I have a Jtree and a custom MouseListener/MouseMotionListener who handles Scrolling on Touch Devices.
My Problem is now that when I start Dragging, the current Node will get instant selected when I'm pressing the mouse.
Question: How can I disable the automatic selection when I drag the mouse (it should be selected when I click on it without dragging).
The clicked
and pressed
functions are empty in my DragscrollListener.
Note:
I haven't coded the DragScrollListener - it can be found here
Sample Code:
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();
}
}
To control the needed behavior when drag is finished, override mouse released in DragScrollListener by extending it. See comments for clarification:
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();
}
}
Don't hesitate to ask if the code is not clear enough.