JTree Filling

I have an array of objects of class A that contain an array of objects of class B. I have quite a few questions: (Encoding examples will be very useful)

  • How can I use JTree with the parent node as an object A and children of the node as B and populate it?
  • Assuming the entire JFrame split into two panels (one containing JTree and another JPanel that displays the attributes of the object corresponding to the option selected on JTree ), how can I do this? At the moment, I can hard code the values ​​in JTree .

I was looking for a lot of examples on the net, but I could only find basic examples.

This is what I have 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 example here 1. Since, for example, the initial format is an array of strings, they use hastable. Since I'm using an object class (A) that contains objects of B, how do I do this (I get the error message above). 2. I attached the layout of my frame. I hardcoded Jtree in the screenshot. What should I do if I click on any Jtree node, I can view the details in J enter image description here TextField next to it?

+4
source share
1 answer

Based on the program snippet and image, you can start by exploring the TreeDemo example discussed in How to Use Trees . Related examples can be found here .

+3
source

All Articles