gpt4 book ai didi

java - JTree : Check the level of selection

转载 作者:行者123 更新时间:2023-11-29 03:51:21 26 4
gpt4 key购买 nike

我正在使用 MouseAdapter 检查 JTree 节点上的双击。我想根据所选节点的级别执行一些不同的操作。如何检查节点的级别?这是监听器的代码:

private MouseAdapter getMouseAdapter(JTree jtree) {
final JTree tree = jtree;
return new MouseAdapter() {

@Override
public void mousePressed(MouseEvent e) {
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if (selPath != null) {
if (e.getClickCount() == 2) {
String selectedNode = selPath.getLastPathComponent().toString();

// >>>>> check on which level of the tree this node is
}
}
}};
}

最佳答案

您可以通过首先调用 getPath() 检查从 selPath 到树根的路径长度selPath 的方法并计算其长度。

Object[] array = selPath.getPath();
int depth = array.length;

关于java - JTree : Check the level of selection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8662102/

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