2014-02-28 73 views
1

我嘗試構建樹,但我無法添加任何葉,只能添加到根。 下面的例子你有什麼幫助嗎? 謝謝,贊布羅塔無法將葉添加到Jtree

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

} 
+0

您是否閱讀過Oracle教程如何使用Trees,代碼示例顯示 - 如何插入節點 – mKorbel

+0

是的,但是在這種情況下,我無法解決問題。 –

+0

:-) .......................... +1 – mKorbel

回答

2

要創建一個多層次的樹,需要向其他節點的子節點添加新的子節點。您正在嘗試使用toReadList來做到這一點。但是,將節點添加到該列表中時,實際上並不是將您添加到樹中的節點,而是新創建的節點。您正在創建添加所有新節點的新樹(稍後放棄)。因此,它們永遠不會以樹頂爲根。

通過

toReadList.add(toTree); 

修復了更換

toReadList.add(new DefaultMutableTreeNode(toWrite)); 

+0

完美的工作,我認爲這是一件小事,但你有花費時間和時間在你總是閱讀的同一部分代碼上。謝謝。 –