gpt4 book ai didi

java - 拖动时禁用 TreeNode Selection

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:22 25 4
gpt4 key购买 nike

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

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

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

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

注意:

我还没有对 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();
}
}

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

关于java - 拖动时禁用 TreeNode Selection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38483738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com