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 Is there a way for updating JTree and JTable with a same model? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is there a way for updating JTree and JTable with a same model?" Watch "Is there a way for updating JTree and JTable with a same model?" New topic
Author

Is there a way for updating JTree and JTable with a same model?

Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Currently I am having two panels with one JTree on left panel and another JTree on right panel. When I click a node of left JTree and click a move button then it will move to the right JTree. I am now able to make this. But now I am having a new requirement in the right panel there should be two options one is JTree and another is JTable. If JTree option is clicked then JTree should display the content or else if JTable is clicked then JTable should display the same content.

So kindly give me a solution for this. Is there a way for updating JTree and JTable with a same model. Or give me any other idea regarding this issue.

Thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Originally posted by Melki Joe:
Is there a way for updating JTree and JTable with a same model. Or give me any other idea regarding this issue.


You would get better results if you thought of reusing the same data instead of the model.

Image the typical windows explorer.
Just like your UI, it displays a JTree on the left and a JTable on the right. However, in the table, since there are multiple columns, you can display more data. However, both the views are still referring to the same data, i.e. your file system.

So all you need to do is build a custom table model which can extract and return data from a given TreeModel.

Typically you will need to override the following methods in your custom model.
1)getColumnCount
2)getColumnName
3)getRowCount
4)getValueAt

Best of luck.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Thank you Godbole for your good suggetion.
Anyway I would be helpfull if I get a sample code regarding this.

Thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Originally posted by Melki Joe:

Anyway I would be helpfull if I get a sample code regarding this.


I was expecting this

Come on.Its not that difficult. Look at the methods I mentioned.
1)getColumnCount. If this returns 3, the table will display 3 columns. I dont know your requirement. It is for you to decide how many columns need to be displayed.
2)getColumnName. Obviously the user would expect some name to the column. Depends on 1 above
3)getRowCount. Now this can be a bit tricky. In a tree you can collapse nodes. So what exactly is your requirement? When the user switches to table view, do you want to display only rows for visible tree nodes or even for nodes that are hidden? This method will have to return the row count accordingly.
4)getValueAt. This is the simplest one, but again depends on your requirement. What information do you need to display in the columns? Typically you have say a File object and you want to display the path in the 1st column, the size in the 2nd column and say last modified date in the 3rd column. The arguments for this method are the row count and the column count. So depending on those, you can return the relevant data.

Why dont you give it a try? You already have your table model. Just pass it to the table model you build, and pull out data for these methods from that model.
In case you get stuck, post your relevant code. And dont forget to use the CODE tags

Best of luck.
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Thank you Godbole.
Let me try and let you know.

Thank you.
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Hi Godbole,

I am able to make designs as you said, but before going to that, when I click a JTree node and click move button then the the node should move to the other Jtree under the same parent node were the leaf node comes from. If there is no such parent node in the other JTree then it should create that node and want to move the leaf node under it. Give your idea on this.

Thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Originally posted by Melki Joe:
Give your idea on this.


Take a look at this.
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Hi

That link did nothing favour to me. My expectation is moving one JTree values to another JTree without changing its parent child structure but only the selected values should move.

Thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Originally posted by Melki Joe:
Hi

That link did nothing favour to me. My expectation is moving one JTree values to another JTree without changing its parent child structure but only the selected values should move.

Thank you.


The link does have the following things (among others):
1) Figuring out the selected node
2) Adding a node dynamically to a tree

That not helpful enough?
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Hi Godbole,

Its all right, actually I am already having the code for moving nodes but I am having three parent nodes. How to identify each. Is there a way by giving path itself that it can identify the parent nodes and move to the corresponding place where it should go.

Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way for updating JTree and JTable with a same model?