无法将叶子添加到Jtree

时间:2014-02-28 09:26:46

标签: java swing jtree defaulttreemodel

我尝试构建一个Tree,但我不能添加任何叶子,只能添加到root。 下面是一个例子你有任何帮助吗? 谢谢,Gianluca

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class ExtendePedigree extends JPanel{

private JTree tree;
private static String lineStyle = "Horizontal";
private static boolean useSystemLookAndFeel = false;
private JPanel descPanel;
private JTextField anelloText;
private JTextField varieta;
private int toReturn;


public  ExtendePedigree(String soggetto){
    JDialog extPedigreeJDialog = new JDialog();
    //Create the nodes.
    DefaultMutableTreeNode top =
        new DefaultMutableTreeNode(soggetto);
    createNodes(top,soggetto);
    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode
            (TreeSelectionModel.SINGLE_TREE_SELECTION);
    //Listen for when the selection changes.
    //tree.addTreeSelectionListener(this);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);        
    descPanel = new JPanel();
    //descPane.setEditable(false);
    JLabel anello = new JLabel("Anello");
    anelloText = new JTextField(20);
    JLabel varietaLabel = new JLabel("Descrizione");
    varieta = new JTextField(50);
    descPanel.add(anello);
    descPanel.add(anelloText);
    descPanel.add(varietaLabel);
    descPanel.add(varieta);       
    JScrollPane descView = new JScrollPane(descPanel);
    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(descView);

    Dimension minimumSize = new Dimension(1024, 768);
    descView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(768/2); 
    splitPane.setPreferredSize(new Dimension(1024, 768));

    if (useSystemLookAndFeel) {
        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Couldn't use system look and feel.");
        }
    }
    extPedigreeJDialog.add(splitPane);
    extPedigreeJDialog.setSize(1024, 768);
    extPedigreeJDialog.setLocationRelativeTo(null);
    extPedigreeJDialog.setModal(true);
    extPedigreeJDialog.setVisible(true);
}

这里我添加到树

private void createNodes(DefaultMutableTreeNode top, String a) {
    DefaultMutableTreeNode nodo = null; 
    ArrayList<DefaultMutableTreeNode> toReadList = new ArrayList<DefaultMutableTreeNode>();
    toReadList.add(new DefaultMutableTreeNode(a));
    Boolean flag = true;
    int j=0;
    while(flag){
        flag=false;
        for(int k=0;k<(Math.pow(2, (j)));k++){
            String b=toReadList.get(0).toString().substring(4);
            System.out.println("j="+j+" k=" + k + " " + " b="+b);
            //DefaultMutableTreeNode toTree = new DefaultMutableTreeNode();
            for(int i=0;i<2;i++){ // leggo genitori soggetto
                String toWrite = new String();
                nodo=toReadList.get(0);
                if((i&1)==0){
                    if (getPadre(b)!=null){
                        flag=true;
                        toWrite="1.0-" + getPadre(b);
                    }
                    else{
                        toWrite="1.0-";                        }
                }
                else{
                    if (getMadre(b)!=null){
                        flag=true;
                        toWrite="0.1-" + getMadre(b);
                    }
                    else{
                        toWrite="0.1-";
                    }
                }                   
                DefaultMutableTreeNode toTree = new DefaultMutableTreeNode(toWrite);
                System.out.println("toTree=" + toTree);
                if(j==0){
                    top.add(toTree);
                    System.out.println("i="+i+"Add to top");
                }
                else{
                    System.out.println("Node="+nodo+"To add="+toTree);
                    nodo.add(toTree);
                }
                toReadList.add(new DefaultMutableTreeNode(toWrite));
            }
            toReadList.remove(0);
            System.out.println("206 flag=" + flag + " k=" + k + " toRead size=" + toReadList.size());
            for(int i=0;i<toReadList.size();i++)
                System.out.println(i + " " + toReadList.get(i));
        }
        j=j+1;
    }
}

这里是主要的,只是为了显示面板

  private static void createAndShowGUI() {
    if (useSystemLookAndFeel) {
        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Couldn't use system look and feel.");
        }
    }

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

    //Add content to the window.
    frame.add(new ExtendePedigree("0.0-FOI-89LR-E-14-13"));

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


        public static void main(String[] args) {
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });

        }                

这些只是运行此示例的方法。

private Object getPadre(String b) {
    toReturn++;
    if(toReturn<20)
        return("1.0-FOI-89LR-E-12-001");
    return(null);
}

private Object getMadre(String b) {
    toReturn++;
    if(toReturn<20)
        return("0.1-FOI-89LR-E-12-002");
    return(null);
}

}

1 个答案:

答案 0 :(得分:2)

要创建具有多个级别的树,您需要将新子项添加到其他节点的子级。您正尝试使用toReadList执行此操作。但是,在向该列表添加节点时,实际上并未将您添加到树中的节点,而是新创建的节点。您正在创建要添加所有新节点的新树(稍后将丢弃)。因此,它们永远不会以树冠为根,最终以树为终结。

更换

toReadList.add(new DefaultMutableTreeNode(toWrite));

通过

toReadList.add(toTree);

解决了这个问题。