Oliver,

It's not difficult but you may need to break the problem down a bit if you
are new to some of this stuff. Then we can address any bits you really need
help with.

Broadly speaking, you could:
1 - create a simple database schema to experiment with and populate the
relevant table(s) with just enough data. Bear in mind that there are whole
books/doctoral-theses on hierarchical data structures in RDBMS and related
SQL techniques so keep your first example as simple as possible (eg a single
table with two text columns representing the relationship between your
nodes, say "parent" and "child", and keep nodes unique eg "Bob" should
appear no more than once in once under "child" for its single, corresponding
"parent", and "Bob" will appear as many times under "parent" as it has
children. Your "root" node should not appear under "child"! You can get much
more sophisticated/powerful/scalabe etc later. 

2 - prepare your JDBC/iBatis/JPA/Hibernate/SpringJDBC/Guice/Warp/Spring...
to access your database and extract the data as required and test that this
works. In essence, all you'll be doing is executing queries along the lines
of 
"SELECT child FROM mytable WHERE parent = ?"

3 implement a TreeModel (or extend/use AbstractTreeModel or
DefaultTreeModel) that uses whatever DAO (or direct data access) you set up
in step 2. In the most naive/simple implementation using JDBC, your getChild
implementation could 

make a connection, 
run the query above (the parent will be passed in), 
create a list from the result set,
return the child corresponding to the index passed in

Of course this would be a crazy solution but once you've got everything
working (you can test that same model in Swing and/or Wicket), you can start
worrying about lazy vs eager loading of nodes, caching,
adding/moving/editing nodes, n-tier architecture and plenty of other stuff a
real-world implementation needs to take account of.

Let us know how you get on once you given that (along with the stuff in my
previous post) a go.

Regards - Cemal 
jWeekend 
OO & Java Technologies, Wicket Training and Development 
http://jWeekend.com


Oliver-Sven Fritsch wrote:
> 
> 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]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Tree-based-on-database-tp25004992p25007026.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to