Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Setting JTree in one class file from another class file

843807Jun 20 2010 — edited Jun 21 2010
Hello,

I'm new to java. I recently created a project in netbeans and here is one of the java files. I used the IDE to make a split pane, with a tree structure and panel in it.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* AmplifierDesignGUI.java
*
* Created on Jun 20, 2010, 1:18:52 PM
*/

package AmplifierDesign;

import java.util.logging.Level;
import java.util.logging.Logger;




/**
*
* @author Bugz
*/
public class AmplifierDesignGUI extends javax.swing.JFrame {

/** Creates new form AmplifierDesignGUI */
public AmplifierDesignGUI() {
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() {

jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jPanel1 = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(jTree1);

jSplitPane1.setLeftComponent(jScrollPane1);

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 475, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 274, Short.MAX_VALUE)
);

jSplitPane1.setRightComponent(jPanel1);

jMenu1.setText("File");
jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(8, 8, 8)
.add(jSplitPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 571, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(8, 8, 8)
.add(jSplitPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 278, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AmplifierDesignGUI().setVisible(true);
try {
new JTreeStructure().setVisible(true);
} catch (Exception ex) {
Logger.getLogger(AmplifierDesignGUI.class.getName()).log(Level.SEVERE, null, ex);
}




}
});
}



// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTree jTree1;
// End of variables declaration


}

So once this was done I wanted to link the JTree to a mysql database. So I found a sample .java file on the net:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package AmplifierDesign;
import java.awt.*;
import java.sql.*;
import java.util.*;

import javax.swing.*;
import javax.swing.tree.*;




public class JTreeStructure extends JFrame {

Connection con = null;
Statement st = null;
ResultSet rs = null;

//public static void main(String args[]) throws Exception {
// new JTreeStructure();
//}

public JTreeStructure() throws Exception {

super("Retrieving data from database ");

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:8889/";
String db = "icons";

ArrayList list = new ArrayList();
list.add("Laser Objects");

Class.forName(driver);
con = DriverManager.getConnection(url + db, "root", "root");

try {
String sql = "Select * from fiberComponents";

st = con.createStatement();
rs = st.executeQuery(sql);

while (rs.next()) {
Object value[] = {"Fiber Components",rs.getString(2) };
list.add(value);
}
} catch (Exception e) {
System.out.println(e);
}
rs.close();
st.close();
con.close();

Object hierarchy[] = list.toArray();

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();

DefaultMutableTreeNode root = processHierarchy(hierarchy);
JTree tree = new JTree(root);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(275, 300);
setLocation(300, 100);
setVisible(true);
}

private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
DefaultMutableTreeNode child;
for (int i = 1; i < hierarchy.length; i++) {
Object nodeSpecifier = hierarchy;
if (nodeSpecifier instanceof Object[]) // Ie node with children
{
child = processHierarchy((Object[]) nodeSpecifier);
} else {
child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
}
node.add(child);
}
return (node);
}
}

The problem is when I run my program two windows open up. The original one with JTree1, and the panel and horizontal splitplane and another window with a new tree component that did get its objects from the database. My question is how do I "replace" the JTree1 with the new tree created from the second java file?
Or additionally, maybe I could set the data for JTree1 from within the second java file?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2010
Added on Jun 20 2010
6 comments
96 views