首页
学习
活动
专区
圈层
工具
发布

填充JTree
EN

Stack Overflow用户
提问于 2011-10-15 04:14:49
回答 1查看 9.2K关注 0票数 4

我有一个A类的对象数组,其中包含一个B类的对象数组,我有很多问题:(编写示例会有很大帮助)

  1. 如何使用以父节点为对象A、子节点为B的JTree并填充它?
  2. 假设整个JFrame被分成两个面板(一个包含JTree,另一个JPanel显示与在JTree上选择的选项相对应的对象属性),那么如何实现呢?到目前为止,我能够将这些值硬编码到JTree中。

我在网上搜索了一个lot中的示例,但是只能找到基本的示例。

这就是我到目前为止所做的:

代码语言:javascript
复制
public class A {
int a1=10;
int a2=20;
B bobj[]=new B[2];
A(){
   bobj[0]=new B();
   bobj[1]=new B();
}
}

class B {
int b=30;
}

在我的Jtree代码中:

代码语言:javascript
复制
import javax.swing.tree.TreeModel;

public class try1 extends javax.swing.JFrame {
 static A a2=new A();
/** Creates new form try1 */
public try1() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    Tree = new javax.swing.JTree();
    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    Tree.setModel(a2);
    Tree.setAutoscrolls(true);
    Tree.setRootVisible(true);
    jScrollPane1.setViewportView(Tree);
    Tree.getAccessibleContext().setAccessibleName("");
    Tree.getAccessibleContext().setAccessibleDescription("");

    jPanel1.setBackground(new java.awt.Color(254, 254, 254));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 655, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 569, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 463, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 151, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(21, 21, 21))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(473, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(43, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE))
            .addContainerGap(24, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    a2=new A();
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
           new try1().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
public javax.swing.JTree Tree;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration

}

我找到了这个示例这里 1,因为eg的初始格式是一个字符串数组,所以它们使用的是hastable。由于我使用的是一个包含B对象的对象类(A),我应该如何做(上面有一个错误)。2.我附上了我的画框的布局。我在截图中对Jtree进行了硬编码。如果我单击任何Jtree节点,我应该如何做才能查看J的详细信息

TextField在它附近吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-15 19:23:42

根据您的程序片段和映像,您可能希望从学习TreeDemo示例开始,该示例在http://download.oracle.com/javase/tutorial/uiswing/components/tree.html中讨论。相关的例子可以找到这里

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7775695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档