tags:

views:

863

answers:

7

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 ?

+2  A: 
akf
A: 

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

iny
+1  A: 

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.

Kathy Van Stone
A: 

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.

jackrabbit
+1  A: 

Does this tutorial help: http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html

User1
A: 

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.

broschb
A: 

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.

Jason S