我正在使用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
}
}
}};
}
最佳答案
您可以通过首先调用selPath
的getPath()方法并计算其长度来检查从selPath
到树根的路径的长度。
Object[] array = selPath.getPath();
int depth = array.length;
关于java - JTree:检查选择级别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8662102/