2016-04-03 65 views
1

我一直在研究文件服务器程序一段时间,到目前为止,我已经能够避免在此处发布某些内容以获得帮助。但我找不到任何关于我的问题,我很困惑。JTree节点编辑路径比较始终为真

我添加了一个弹出菜单,其中包含创建新顶层文件夹的选项,它真的只是创建一个节点,并在其编辑后将其名称发送到服务器以创建文件夹。尽管我已经完成了所有编辑工作,并且上传正常,但我遇到了问题。

我将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)); 
} 

不幸的是,当检查tree.getEditingPath().equals(nfPath)总是返回true,因此它仍然编辑。

但我不明白为什么它仍然是真的,它显然不应该。如果它帮助/改变任何东西,这是运行在一个单独的线程(否则,while循环将停止渲染图形用户界面)

那么我应该/我可以做什么,有更好的方式来做到这一点,或在至少有一个工程?

UPDATE:

虽然我还没有找到一个解决上述问题的明确,如果我为tree.isPathSelected(nfPath)而不是测试工作正常,树被设置为不可编辑之后!

回答

1

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

改为使用tree.isPathSelected(path)将工作