JTree-如何使用For-Loop添加节点? [英] JTree - how to add nodes with a For-Loop?

查看:136
本文介绍了JTree-如何使用For-Loop添加节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JTree,可以系统地添加来自相关var的节点:

I have a simple JTree, that systematically adds nodes from relevant vars:

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-Loop,以避免额外的选择.

what I would like, is to use a For-Loop, to avoid extra 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] );
}

我该怎么做?

谢谢

推荐答案

解决方案非常简单:

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

sections [i]需要使用方括号

sections[i] needs double brackets

这篇关于JTree-如何使用For-Loop添加节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆