ddd
ddd

Reputation:

Synchronizing JTable and JTree

Is there a way for a JTable and a JTree to share the same model, so that any change in that underlying model is reflected immediately in both components ?

Upvotes: 4

Views: 3698

Answers (6)

akf
akf

Reputation: 39485

I am not sure if this is what you are looking for, but there is a two part tutorial from Sun on creating tree tables, which is essentially a JTable with a JTree renderer in the first column. tutorial links: part 1 part 2

EDIT:

On the topic of a TreeTable, (a Table component that maintains a tree in its first column, which allows for the hiding and displaying of rows based on the user's modifications of the Tree) I have found an implementation by NetBeans called Outline. It is very easy to use. A simple example took less than 30 minutes to mock up. The code can be found in this answer.

Here is an image of the TreeTable:

alt text http://img17.imageshack.us/img17/6643/picture1hz.png

Upvotes: 4

Jason S
Jason S

Reputation: 189626

Take a look at GlazedLists -- there's an ability to use an EventList for both a JTable and a JTree. I'm not familiar with the JTree rendering, but the JTable part of GlazedLists is pretty solid.

Upvotes: 0

broschb
broschb

Reputation: 5006

It's been stated, the best way is to create a datastructure(model) of some kind to represent your data, and then have the treemodel, and tablemodel look to the common datastructure to pull the data. Doing this will allow both of them to share the same model, you will just need to fire the correct events when data changes so that both of them are updated.

Upvotes: 0

jackrabbit
jackrabbit

Reputation: 5653

Assuming you want tree nodes containing the properties of each record and one table row per record, it shouldn't be too hard to create adapters for the TableModel and TreeModel interfaces based on a list of records.

Upvotes: 0

Kathy Van Stone
Kathy Van Stone

Reputation: 26271

If you have a type Obj that can be represented both as a tree and a table, you can either create a TableModel and a TreeModel that observe changes to Obj and respond accordingly, you can make Obj implement both TableModel and TreeModel (although I don't like business objects implementing GUI objects), or you can create a class that implements both TableModel and TreeModel and knows when changes to Obj happen.

Upvotes: 2

iny
iny

Reputation: 7591

The interfaces are different, but it should be totally doable to implement them using the same data structure below.

Upvotes: 0

Related Questions