Hi everybody!
Still new to Wicket I'm trying to get a wicket tree with nodes from a
database. What I got so far is a simple JTree put onto a JFrame. What I
don't understand is how to geht my tree onto a wicket web page. I'm
kinda confused on how to get things working. As far as I now wicket tree
uses jtree aswell, but I'm really stuck at this point. I'd really
appreaciate any help from anyone pointing me into the right direction.
public class MyJTree extends JFrame {
Connection con = null;
Statement st = null;
ResultSet rs = null;
public static void main(String args[]) throws Exception {
new MyJTree();
}
public MyJTree() throws Exception {
super("Retrieving data from database ");
MyConnection mycon = MyConnectionFactory.getNewConnection();
mycon.connect();
ArrayList list = new ArrayList();
list.add("The Root");
try {
String sql = "select key, node_id, parent_id, caption from
mytable";
st = mycon.createPreparedStatement(sql);
rs = st.executeQuery(sql);
while (rs.next()) {
Object value[] = {rs.getString(1), rs.getString(2),
rs.getString(3), rs.getString(4)};
list.add(value);
}
} catch (Exception e) {
System.out.println(e);
}
rs.close();
st.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[i];
if (nodeSpecifier instanceof Object[]) // Ie node with children
{
child = processHierarchy((Object[]) nodeSpecifier);
} else {
child = new DefaultMutableTreeNode(nodeSpecifier); // Ie
Leaf
}
node.add(child);
}
return (node);
}
}
Thanks!
Oliver
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]