簡體   English   中英

JTree 節點編輯路徑比較始終為真

[英]JTree node editing path comparison always true

我已經在文件服務器程序上工作了一段時間,到目前為止,我已經能夠避免在此處發布一些內容以尋求幫助。 但是我找不到任何關於我的問題的東西,我很困惑。

我添加了一個彈出菜單,其中包含創建新頂級文件夾的選項,它實際上只是創建一個節點,並在編輯后將其名稱發送到服務器以創建文件夾。 雖然我的所有編輯工作都正常工作並且上傳工作正常,但我遇到了問題。

在創建文件夾時,我將 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));
}

不幸的是,while 檢查tree.getEditingPath().equals(nfPath)總是返回true ,因此它仍然是可編輯的。

但我不明白為什么它仍然如此,顯然不應該。 如果它有幫助/改變任何東西,它會在一個單獨的線程中運行(否則 while 循環會阻止 GUI 渲染)

那么我應該/可以做什么,有沒有更好的方法來做到這一點,或者至少有一個有效的方法?

更新:

雖然我還沒有找到解決上述明確問題的解決方案,但如果我改為測試tree.isPathSelected(nfPath)效果很好,並且之后樹被設置為不可編輯!

獲取編輯路徑不會刪除正在編輯的路徑的變量...因此,在完成編輯后,最近編輯的路徑仍然是正確的路徑。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM