填充JTree

时间:2011-10-15 04:14:50

标签: java swing jpanel jtree

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

  1. 如何将JTree与父节点一起用作对象A,将子节点用作B并填充它?
  2. 假设整个JFrame分为两个面板(一个包含JTree,另一个JPanel显示对应于JTree上所选选项的对象属性我怎么能做到这一点?截至目前,我能够将值硬编码到JTree
  3. 我在网上搜索了一个很多的例子但是只能找到基本的例子。

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

    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
    
    }
    

    我找到了这个例子here 1.由于eg的初始格式是一个字符串数组,因此它们使用的是hastable。由于我使用的是包含B对象的一类对象(A),我应该怎么做(我上面得到了一个错误)。 我已经附上了我的Frame的布局。我在屏幕截图中硬编了Jtree。我应该怎么做,如果我点击任何Jtree节点,我能够查看它附近的J enter image description here TextField的详细信息?

1 个答案:

答案 0 :(得分:3)

根据您的程序片段和图片,您可能需要先研究TreeDemo中讨论的How to Use Trees示例。可以找到相关示例here