我正在使用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
}
}
}};
}
答案 0 :(得分:4)
您可以首先调用selPath
selPath
方法并计算其长度来检查从Object[] array = selPath.getPath();
int depth = array.length;
到树根的路径长度。
{{1}}
答案 1 :(得分:0)
TreePath path = tree.getSelectionPath();
int level = path.getPathCount();