JTable and JTree Sync

Is there a way for JTable and JTree to use the same model so that any changes in this base model are immediately reflected in both components?

+4
source share
7 answers

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

EDIT:

On the topic TreeTable (a table component that supports a tree in its first column, which allows you to hide and display rows based on custom tree modifications), I found a NetBeans implementation called Outline . It is very easy to use. A simple example took less than 30 minutes to mock. Code can be found in this answer .

Here is a picture of a TreeTable:

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

+4
source

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

+2
source

The interfaces are different, but they must be fully feasible for their implementation using the same data structure as below.

0
source

Assuming that you need tree nodes containing the properties of each record and one row of the table per record, it should not be too difficult to create adapters for the TableModel and TreeModel interfaces based on the list of records.

0
source

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

0
source

Take a look at GlazedLists - you can use EventList for JTable and JTree. I am not familiar with JTree rendering, but the JTable part of GlazedLists is pretty solid.

0
source

Source: https://habr.com/ru/post/1285979/


All Articles