2014-03-27 29 views
0

我試圖在重新加載後在JTree中找到解決問題的方法。 情況:JTree在重新加載後避免崩潰

JTree的

[-] Office A 
|---[-] Office A.1 
| |---[-] Office A.1.1 
| |---[-] Office A.1.2 
[-] Office B 
|---[-] Office B.1 
| |---[-] Office B.1.1 
| | |---[-] Office B.1.1.1 

現在我必須添加Office A.1.3。要做到這一點,我得到Office A.1和方法add(DefaultMutableTreeNode aNode)我加Office A.1.3

OfficeA1.add(OfficeA13); 

在此之後我呼籲樹的DefaultTreeModelreload方法。

的問題是,在此之後調用塌樹都:

[+] Office A 
[+] Office B 

我必須手動展開節點Office A,以確保該節點添加...

[-] Office A 
|---[-] Office A.1 
| |---[-] Office A.1.1 
| |---[-] Office A.1.2 
| |---[-] Office A.1.3 
[+] Office B 

我的代碼...

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root not visibile"); 
    DefaultMutableTreeNode usersRoot = new DefaultMutableTreeNode("Utenti"); 
    DefaultMutableTreeNode groupsRoot = new DefaultMutableTreeNode("Gruppi"); 
    DefaultMutableTreeNode officesRoot = new DefaultMutableTreeNode("Uffici") 
    root.add(usersRoot); 
    root.add(groupsRoot); 
    root.add(officesRoot); 

    JTree ccTree = new JTree(root); 

,當我添加節點...

Office anOffice = //get the correct office object 
DefaultTreeModel model = (DefaultTreeModel)competenzaTree.getModel(); 
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot(); 

DefaultMutableTreeNode n = (DefaultMutableTreeNode)root.getChildAt(0); 

n.add(new DefaultMutableTreeNode(anOffice)); 
model.reload(n); 

問題是惠普officesRoot節點。 usersRootgroupsRoot節點不是分層的。

有沒有辦法避免這種行爲? 謝謝。

也許另一種要問的方法可能是這是從樹上添加/刪除節點而不會導致所有樹崩潰的方式?

p.s.我也讀this post但它沒有幫助我。

+0

發現考慮使用DefaltTreeModel#nodesWereInserted或者DefaultTreeModel的#insertNodeInto這可能是添加 – MadProgrammer

+0

代碼......並且,你可以閱讀更新我的意思是添加一個節點和重新加載樹更容易...是不夠清楚? – Fry

+0

@MadProgrammer你可以在答案中更好地解釋,所以我可以嘗試你的建議? – Fry

回答

2

考慮使用DefaultTreeModel#insertNodeInto(DefaultMutableNode, DefaultMutableNode, int)應告知JTree表,一個新的節點變得可用,導致要更新的JTree,但不應該影響

這個例子是基於樹的當前展開狀態例如,在How to use trees

import java.awt.BorderLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 

import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.TreeSelectionModel; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.TreePath; 

public class TestTree extends JPanel { 

    private JTree tree; 
    private DefaultTreeModel model; 
    private JButton btnAdd; 
    private int childCount; 

    public TestTree() { 
     super(new BorderLayout()); 

     //Create the nodes. 
     DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); 
     createNodes(top); 

     model = new DefaultTreeModel(top); 

     //Create a tree that allows one selection at a time. 
     tree = new JTree(model); 
     tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 

     //Create the scroll pane and add the tree to it. 
     JScrollPane treeView = new JScrollPane(tree); 

     //Add the split pane to this panel. 
     add(treeView); 

     btnAdd = new JButton("Add"); 
     btnAdd.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       TreePath treePath = tree.getSelectionPath(); 
       if (treePath != null) { 
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent(); 
        DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child " + (++childCount)); 
        model.insertNodeInto(child, node, node.getChildCount()); 
       } 
      } 
     }); 

     add(btnAdd, BorderLayout.SOUTH); 
    } 

    private class BookInfo { 

     public String bookName; 

     public BookInfo(String book) { 
      bookName = book; 
     } 

     public String toString() { 
      return bookName; 
     } 
    } 

    private void createNodes(DefaultMutableTreeNode top) { 
     DefaultMutableTreeNode category = null; 
     DefaultMutableTreeNode book = null; 

     category = new DefaultMutableTreeNode("Books for Java Programmers"); 
     top.add(category); 

     //original Tutorial 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial: A Short Course on the Basics")); 
     category.add(book); 

     //Tutorial Continued 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial Continued: The Rest of the JDK")); 
     category.add(book); 

     //JFC Swing Tutorial 
     book = new DefaultMutableTreeNode(new BookInfo("The JFC Swing Tutorial: A Guide to Constructing GUIs")); 
     category.add(book); 

     //Bloch 
     book = new DefaultMutableTreeNode(new BookInfo("Effective Java Programming Language Guide")); 
     category.add(book); 

     //Arnold/Gosling 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Programming Language")); 
     category.add(book); 

     //Chan 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Developers Almanac")); 
     category.add(book); 

     category = new DefaultMutableTreeNode("Books for Java Implementers"); 
     top.add(category); 

     //VM 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Virtual Machine Specification")); 
     category.add(book); 

     //Language Spec 
     book = new DefaultMutableTreeNode(new BookInfo("The Java Language Specification")); 
     category.add(book); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, this method should be 
    * invoked from the event dispatch thread. 
    */ 
    private static void createAndShowGUI() { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } 

     //Create and set up the window. 
     JFrame frame = new JFrame("TreeDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add content to the window. 
     frame.add(new TestTree()); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event dispatch thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

}