簡體   English   中英

如何使用字符串JTree創建節點?

[英]How to create node using string JTree?

為了創建JTree並僅通過了解字符串路徑插入新節點,我編寫了以下幾行:

private JFrame frame;
JTree tree;
DefaultMutableTreeNode root;
DefaultTreeModel model;

public static void main(String[] args)
{
    test t = new test();
    t.add_new_folder("Data","trial 0");
    t.add_new_folder("Data/trial 0","trial 1");
    t.frame.revalidate();
    t.frame.repaint();

}
public test()
{
    frame = new JFrame();
    tree = new JTree();
    root = new DefaultMutableTreeNode("Data");
    model = new DefaultTreeModel(root);
    tree.setModel(model);
    frame.getContentPane().add(tree, BorderLayout.WEST);
    frame.setVisible(true);
    frame.setSize(500, 500);
}

直到這里一切看起來都不錯並且可以正常工作,但是當調用此方法時,它並沒有達到預期的結果

    public void add_new_folder(String path,String name){
    String[] data = path.split("/");
    TreePath t = new TreePath(data);
    DefaultMutableTreeNode parent = new DefaultMutableTreeNode(t);
    model.insertNodeInto(new DefaultMutableTreeNode(name), parent, parent.getChildCount());
    model.reload();
}

這就是我得到的

在此處輸入圖片說明

那怎么解決呢?

試試這個代碼:

public class Test {
    private JFrame frame;
    JTree tree;
    DefaultMutableTreeNode root;
    DefaultTreeModel model;
    private Map<String, DefaultMutableTreeNode> treeMap;

    public static void main(String[] args)
    {
        Test t = new Test();
        t.add_new_folder("Data","trial 0");
        t.add_new_folder("Data/trial 0","trial 1");
        t.frame.revalidate();
        t.frame.repaint();
    }

    public Test()
    {
        frame = new JFrame();
        tree = new JTree();
        root = new DefaultMutableTreeNode("Data");
        model = new DefaultTreeModel(root);
        tree.setModel(model);
        frame.getContentPane().add(tree, BorderLayout.WEST);
        frame.setVisible(true);
        frame.setSize(500, 500);
        treeMap = new HashMap<>();
        treeMap.put("Data", root);
    }

     public void add_new_folder(String path,String name){
        DefaultMutableTreeNode currentNode = treeMap.get(path);
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(name);
        currentNode.add(childNode);
        treeMap.put(path+"/"+name, childNode);
        model.reload();
    }
}

我知道這是否是您想要的,但可以帶您跟隨;)

嘗試遍歷特定節點並獲取TreePath。 以下代碼將為您提供幫助。

    public void add_new_folder(String path,String name){
        String[] data = path.split("/");

        TreePath tPath = findPath(data[data.length-1]);
        if(tPath != null){
            ((DefaultMutableTreeNode)tPath.getLastPathComponent()).add(new DefaultMutableTreeNode(name));
        }
    }

    @SuppressWarnings("unchecked")
    private TreePath findPath(String s) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = e.nextElement();
            if (node.toString().equalsIgnoreCase(s)) {
                return new TreePath(node.getPath());
            }
        }
        return null;
    }

暫無
暫無

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

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