Thanks for your reply. I guess defining the TreeModel is exactly my
problem. I tried to find a tutorial on how to do so based on my database
entries but I don't seem to find any. Maybe this is basic java knowledge
and I'm searching at the wrong spots. But a little how to would be great
because right now I have no idea how to solve my problem. I guess this
is a common use case while handling Trees on web apps, yet wondering why
there isn't a fool-proof guide. Any hints would be greatly appreaciated.
Oliver
Cemal wrote:
Oliver,
Wicket (core and extensions) has several tree components and they all
currently use Swing's TreeModel (javax.swing.tree.TreeModel). I say
currently as this is the case upto and including Wicket 1.4 but there is
some discussion about using a new type of model, more suited to webapps, in
Wicket 1.5. Swing's TreeModel is not coupled to JTree at all even though it
happens to be the model JTree uses. Wicket does _not_ use JTree, but its
trees use TreeModel.
In the context of a Swing UI your code below would be much more flexible,
powerful and in the spirit of Swing if you were to explicitly use a
TreeModel to manage interaction with the underlying data; I expect even
Swing's out-of-the-box AbstractTreeModel (or the provided concrete subclass,
DefaultTreeModel which uses DefaultMutableTreeNode) would suffice for your
use-case based on what the code you've posted is doing.
Once you have created your TreeModel, you can use it with Wicket's trees too
as simply as:
add(new LinkTree("tree", myTreeModel));
for example
See [1] for a simple example of how to use JTree (with a TreeModel), [2] for
a simple Wicket tree example and if you want to learn a bit more about how
some of Wicket's trees work check out this class diagram [3] from a couple
of years ago.
Does that make sense?
Regards - Cemal
jWeekend
OO & Java Technologies, Wicket Training and Development
http://jWeekend.com
[1]
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#dynamic
[2] http://www.wicket-library.com/wicket-examples/ajax/tree/simple.1
[3] http://jweekend.com/dev/ArticlesPage/
Oliver-Sven Fritsch wrote:
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]