문제

I am tryng to build a Tree, but I cannot add any leaf, only be able to add to the root. Below an example do you have any help? Thanks, 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);
}

Here I add to the tree

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;
    }
}

Here the main and just to show the panels

  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();
        }
    });

        }                

Those are methods just to run this example.

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);
}

}
도움이 되었습니까?

해결책

To create a tree with multiple levels you need to add new children to the children of other nodes. You are trying to do that using the toReadList. However, when adding nodes to that list you are not actually taking the node you added to the tree, but a newly created node. You are creating new trees (which you later just discard) to which you are adding all the new nodes. They thus never end up in the tree with top as root.

Replacing

toReadList.add(new DefaultMutableTreeNode(toWrite));

by

toReadList.add(toTree);

fixes that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top