File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTreeTable - tree of DefaultMutableTreeNodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTreeTable - tree of DefaultMutableTreeNodes" Watch "JTreeTable - tree of DefaultMutableTreeNodes" New topic
Author

JTreeTable - tree of DefaultMutableTreeNodes

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I am busy working on the JTreeTable. I have taken the example from the Swing connection site and I am trying to get it going on my own. I'm getting along pretty well, but I'm getting stuck with changing their example to be work for what I need. I don't want to get the code from anyone as I want to learn to do it myself, but I would very much appreciate some suggestions to get me on my way.

In their example, they use their FileSystemModel to populate the tree in the table. I have a tree that is exactly what I want in the table, but I don't know how to get it in there as it is made of the DefaultMutableTreeNode and I have added nodes to nodes to get the tree.

Could anyone give some suggestions to get me started?

Many thanks,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Create a concrete subclass of AbstractTreeTableModel. In it's constructor, it can take the root node of the tree you've created. You should be able to implement all the abstract methods from this. Don't forget to override getColumnClass to return TreeTableModel.class in the column you want the tree to appear.
Good luck, JTree's are nasty, JTreeTable can be horrid !
D.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Thanks for that advice, I now have a sort of TreeTable but at least the tree is in the table which I guess was the biggest hurdle! Hooray!!

Cheers,
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

So I now have a tree in the first column and check boxes in the second column. Now I know that to make the checkboxes editable, I have to use isCellEditable which is in the TreeTableModelAdapter, and then to set the value. So I am using the setValue method in the same class.



but instead of printing out the value, obviously I want to actually set the value at that location. In a normal table I would access the row's information and set the value like that, how do I access the row information in a treetable?

Many thanks,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
what does your getValueAt method look like ?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434


(and tree is a JTree)
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
actually I was thinking of the getValueAt method of your TreeTableModel impl. Can you show ?
the setValueAt method will look something like this getValueAt method.


I don't think you need to subclass TreeTableModelAdaptor. This class will just root setValueAt calls to your impl. of TreeTableModel.

make sense?
D.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
OK, I'm not quite sure that I get it.

Like I said, I'm using amlost exactly the SwingConnections stuff to understand.

So I have
public abstract class AbstractTreeTableModel implements TreeTableModel

and

public class NewsTableModel extends AbstractTreeTableModel implements TreeTableModel


So to answer your question, in the TreeTableModel implementation (ie, AbstractTreeTableModel) there is this written



and in the subclass (NewsTableModel) there is



I'm returning "hello" because I know it should be but I wrote that and forgot about it till now. The tree shows up and works fine anyhow.

Was that the code you needed?

Cheers and thank you so much for your time,
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
aha, so you don't really have a model (merely a mock-up), so there is nothing to set.

Implement getValueAt properly and setValueAt will be fairly obvious.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
OK, I modified the getValue method to look like this...



but I still don't understand how to do the setValue method. I know that I need to reference the same point that is changed, set the new value and fireTableChanged. But I don't know how to reference that point.

Thanks again
Rachel
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Rachael,

Presumbly in your tree, your DefaultMutableTreeNodes have a userObject attribute. If for example, these objects are of type Foo, which have a name and enabled flag, your code would look something like this :



setValueAt then becomes :



hope that helps. I also have a worked example at home that I'll try and post tonight.

D.
[ September 08, 2004: Message edited by: Don Kiddick ]
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Don

I see it now, you actually treat the whole row like a node and that node is an object that you can get attributes out of and put in the columns. I think I get it now!

I'll give it some more coding and I'll let you know how it goes.

Thank you so much for taking the time to help me!

Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Don

I finished coding it last night and it looks beautiful and it works wonderfully. You were right, the JTreeTable is a bugger and hopefully I"ll be able to use this one as a base for the next. But it really does give my user interface that extra edge.

Many kind regards and so many thanks!
Rachel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTreeTable - tree of DefaultMutableTreeNodes