Java JTree节点编辑路径比较始终为true

Java JTree节点编辑路径比较始终为true,java,multithreading,jtree,defaultmutabletreenode,treepath,Java,Multithreading,Jtree,Defaultmutabletreenode,Treepath,我已经在一个文件服务器程序上工作了一段时间,到目前为止,我能够避免在这里发布一些帮助信息。但是我在我的问题上找不到任何东西,我很困惑 我添加了一个弹出菜单,可以选择创建新的顶级文件夹,它实际上只是创建一个节点,在编辑后将其名称发送到服务器以创建文件夹。虽然我已经让所有的编辑工作正常,并有上传工作,我有一个问题 当创建文件夹时,我将JTree更改为可编辑,并执行一个while循环,直到该节点不是正在编辑的节点,此时它将从JTree中删除编辑功能 public static void newTopF

我已经在一个文件服务器程序上工作了一段时间,到目前为止,我能够避免在这里发布一些帮助信息。但是我在我的问题上找不到任何东西,我很困惑

我添加了一个弹出菜单,可以选择创建新的顶级文件夹,它实际上只是创建一个节点,在编辑后将其名称发送到服务器以创建文件夹。虽然我已经让所有的编辑工作正常,并有上传工作,我有一个问题

当创建文件夹时,我将JTree更改为可编辑,并执行一个while循环,直到该节点不是正在编辑的节点,此时它将从JTree中删除编辑功能

public static void newTopFolder(){
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); //now we have the root
    DefaultMutableTreeNode newFolder = new DefaultMutableTreeNode("New Folder");//will change to increment for duplicates
    DefaultMutableTreeNode empty = new DefaultMutableTreeNode("< empty >");  //easier way to have empty folder, don't worry about it
    tree.setEditable(true); //sets to editable
    model.insertNodeInto(newFolder, root, root.getChildCount()); //adds folder to tree
    model.insertNodeInto(empty, newFolder, newFolder.getChildCount()); //adds empty to tree, not real file
    TreePath nfPath = getPath(newFolder); //so we don't call getPath extra times
    tree.startEditingAtPath(nfPath); //automatically selects folder to edit
    System.out.println(tree.getEditingPath().toString()+":"+nfPath.toString()+";"); //returns [\user\, New Folder]:[\user\, New Folder]; which shows the two are equal
    while(tree.getEditingPath().equals(nfPath)){//when nothing is selected null if nothing is being edited, and path to other objects if selected

    }
    tree.setEditable(false); //changes to node will have been committed and editing disable 
    sendFolderToServer(nfPath); //sends folder to server after formatting as a String used in new File(Paths.get(nfPath));
}
publicstaticvoidnewtopfolder(){
DefaultTreeModel模型=(DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode根=(DefaultMutableTreeNode)model.getRoot();//现在我们有了根
DefaultMutableTreeNode newFolder=新的DefaultMutableTreeNode(“新文件夹”);//对于重复项,将更改为增量
DefaultMutableTreeNode empty=新的DefaultMutableTreeNode(“”)//使用空文件夹更简单,不用担心
tree.setEditable(true);//设置为可编辑
model.insertNodeInto(newFolder,root,root.getChildCount());//将文件夹添加到树中
model.insertNodeInto(empty,newFolder,newFolder.getChildCount());//将空添加到树中,而不是实际文件
TreePath nfPath=getPath(newFolder);//所以我们不需要额外调用getPath
tree.startEditingAtPath(nfPath);//自动选择要编辑的文件夹
System.out.println(tree.getEditingPath().toString()+”:“+nfPath.toString()+”;”;//返回[\user\,新文件夹]:[\user\,新文件夹];这表明两者相等
while(tree.getEditingPath().equals(nfPath)){//当未选择任何内容时,如果未编辑任何内容,则为null;如果选择,则为其他对象的路径
}
tree.setEditable(false);//对节点的更改将被提交并禁用编辑
sendFolderToServer(nfPath);//将文件夹格式化为新文件中使用的字符串后发送到服务器(path.get(nfPath));
}
不幸的是,while检查
树.getEditingPath().equals(nfPath)
总是返回
true
,因此它仍然是可编辑的

但我不明白为什么它仍然是真的,它显然不应该是真的。如果它有助于/更改任何内容,它将在单独的线程中运行(否则while循环将停止GUI的渲染)

那么我应该/可以做什么,有没有更好的方法,或者至少有一种有效的方法

更新:


虽然我还没有找到解决上述问题的方法,但是如果我改为测试
tree.isPathSelected(nfPath)
,它工作得很好,树被设置为以后不可编辑

获取编辑路径不会删除正在编辑的路径的变量。。。因此,编辑完成后,最近编辑的路径仍然是正确的路径

而是使用
tree.isPathSelected(path)
将起作用