Reputation: 1301
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)
JTree
with parent node as Object A and children node as B's and populate it?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 JTextField near it?
Upvotes: 4
Views: 9320
Reputation: 205885
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.
Upvotes: 3