2013-07-19 34 views
0

我有一個簡單JTree,該系統增加了節點從相關瓦爾:JTree - 如何使用For-Loop添加節點?

public void init() 
{ 
    final String section1 = "JAVA"; 

    final String section1_content1 = "Tutorial1"; 
    final String section1_content2 = "Tutorial2"; 
    final String section1_content3 = "Tutorial3"; 
    final String section1_content4 = "Tutorial4"; 
    final String section1_content5 = "Tutorial5"; 
    final String section1_content6 = "Tutorial6"; 

    final String content1a = "Introduction"; 
    final String content1b = "Hello World!"; 

    // Create the title node: 
    title = new DefaultMutableTreeNode(section1); 

    // Create and attach the 1st subtree: 
    selection = new DefaultMutableTreeNode(section1_content1); 

    selection.insert(new DefaultMutableTreeNode(content1a),0); 
    selection.insert(new DefaultMutableTreeNode(content1b),0); 

    title.insert(selection,0); 
} 

我想什麼,就是用一個for循環,避免額外selection.inserts

String[] sections = new String[]{ "Tutorial1", "Tutorial2", "Tutorial3", "Tutorial4", "Tutorial5", "Tutorial6" }; 

for (int i=0; i < sections.length; i++) { 
    selection = new DefaultMutableTreeNode(sections[i]); 
} 

我會怎麼做呢?

感謝

+0

看到甲骨文tutorila如何使用樹(http://docs.oracle。 COM/JavaSE的/教程/ uiswing /組件/ tree.html)中,描述了基本的東西,大約 – mKorbel

+0

謝謝你,我已經研究之前對這個問題的例子,我知道節點分別 – user2521443

+0

加入。我敢肯定有將是一種方式將所有節點添加用的代碼只有幾行,所以如果任何人有想法,他們將不勝感激:) – user2521443

回答

0

的解決方案很簡單:

for (int i=0; i<sections.length; i++) { 
    selection = new DefaultMutableTreeNode((sections[i])); 
    title.insert(selection,0); 
    } 

節[I]需要雙括號