Winter Roberts Winter Roberts - 5 months ago 23
Java Question

JTree node editing path comparison always true

I've been working on a file server program for a while, and thus far I've been able to avoid posting something here for help. But I cannot find anything on my problem and I am very confused.

I've added a popup menu with the option to create new top level folder, which really just creates a node and, after its edited sends its name to the server to create the folder. While I've got all of the editing working correctly and have the upload working, I am having a problem.

I change the JTree to be editable when the folder is created, and a while loop that continues until that node is not the one being edited, at which point it removes edit-ability from the 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));
}


Unfortunately, the while check
tree.getEditingPath().equals(nfPath)
always returns
true
, thus it remains editable.

But I don't understand why it remains true, it clearly shouldn't. In case it helps/ changes anything, this is run in a separate thread (otherwise the while loop would stop the GUI from rendering)

So what should / can I do, is there a better way to do this, or at least one that works?

UPDATE:

While I haven't found a solution to the clear problem above, If I instead test for
tree.isPathSelected(nfPath)
That works fine and the tree is set to not be editable afterward!

Answer

Get editing path doesn't remove the variable of the path being edited... Therefore, after it's done editing the most recently edited path is still the correct path.

Instead using tree.isPathSelected(path) will work

Comments