填充 JTree

发布于 2024-12-09 20:10:24 字数 5457 浏览 0 评论 0原文

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

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

我在网上搜索了很多示例,但只能找到基本示例。

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

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 代码中:

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。由于我使用的对象类(A)包含 B 的对象,我应该怎么做(上面出现错误)。 2.我附上了我的框架的布局。我已经在屏幕截图中对 Jtree 进行了硬编码。我应该怎么做,这样如果我单击任何 Jtree 节点,我就可以查看 J在此处输入图像描述TextField 附近吗?

I'm having an array of objects of class A which contain an array of objects of class B.
I've got quite a few questions: (Coding examples would be of great help)

  1. How can I use a JTree with parent node as Object A and children node as B's and populate it?
  2. Assuming that the entire JFrame is divided into two panels(one containing the JTree and another JPanel which displays object's attributes corresponding to the option selected on the JTree) how can I make this happen? As of now, I'm able to hard-code the values into the JTree.

I've searched a lot for examples on the net but was able to find only basic examples.

This is what I've done so far:

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

In my Jtree code:

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

}

I found this examplehere
1. Since the eg's initial format is an array of strings,they're using the hastable. Since I'm using a class of objects(A) which contains objects of B, how should I do it(I'm getting an error above).
2.I've attached the layout of my Frame. I've hardcoded the Jtree in the screenshot. What should I do so that if I click on any Jtree node, I'm able to view the details on Jenter image description hereTextField near it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-12-16 20:10:24

根据您的程序片段和图像,您可能需要首先研究 TreeDemo 中讨论的示例 如何使用树。相关示例可以在此处找到。

Based on your program fragment and image, you may want to start by studying the TreeDemo example discussed in How to Use Trees. Related examples may be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文