Swing-agnostic Table&Tree models

Hello,

several questions in one, really. They assume familiarity with Swing's Model classes, but are hopefully more general.

Swing's JTable widget displays data found in an instance of a class implementing TableModel. Swing provides a default implementation (a Vector of Vector), but I find it safer to provide custom TableModel classes for providing my data.

To expose your app-specific data through a custom TableModel class, you can either:

1) store the data in an app-specific TableModel implementation

2) store your data in an app-specific structure, with no dependency on Swing interfaces, and provide a custom TableModel implementation as an Adapter over your app-specific structure.

3) = 2) plus a Controller/Mediator that knows when data is modified

I find solution 1 clumsy, as this makes the business data model depend on a UI package.

But solution2 is a bit tedious. The adapter has to be both ways, so that you can provide the data when the JTable asks them (e.g. to repaint() the table) , and also notify the JTable when your data has changed (the JTable is an observer of its TableModel).

You end up designing a tabular data structure with much of the TableModel functionalities (get() accessors and fireXxxChanged() notifiers), in addition to the the adapter.

Solution 3 uses Controller or Mediator classes, through which all of the data modification requests should flow.

Such classes know about both the app-specific model and the Adapter, and notify the latter when they modify the former.

That spares the app-specific data to worry about notifying changes, but is not much cheaper that solution 2 in terms of maintenance overhead (after each data modification, the controller has to notify the TableModel, much like in solution 2, the business model has to notify the adapter).

Questions:

- Do you have a recommended approach?

- Specifically, do you go for solution 1, or are you worried that a business data class has dependencies on Swing classes (be they model classes)?

- Do you think it would make sense to add Tree and Table classes in the standard collection classes? I have started filing a similar RFE and I wonder whether it deserves lobbying :o)

The afterthought of the last question is, if such classes as the XxxModel had been standard java.util classes, I wouldn't be worried at all by solution 1.

[2450 byte] By [jdupreza] at [2007-10-3 9:31:56]
# 1

I've thought about this problem for as loing as my first Swing app, and never could set up a policy about this.

My current Swing app is a business data explorer, with one big navigational tree, and correlated tables displaying different data types at all floors, and I'm afraid solution 2 won't scale...

jdupreza at 2007-7-15 4:46:53 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I'm surprised nobody answered this one.

I also had the same question recently on this forum.

http://forum.java.sun.com/thread.jspa?threadID=761180&tstart=90

In the end, in my particular example, I went for your option 2 (approximately). Looking back, I still think that was probably the right choice, but I was never entirely happy with it.

TimRyanNZa at 2007-7-15 4:46:53 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> I'm surprised nobody answered this one.

Thanks for comforting :o)

Especially after the Haka rang so loud in Lyons 2 days ago...

> I also had the same question recently on this forum.

> http://forum.java.sun.com/thread.jspa?threadID=761180&

> tstart=90

> In the end, in my particular example, I went for your

> option 2 (approximately). Looking back, I still

> think that was probably the right choice, but I was

> never entirely happy with it.

Quoting duffymo in that thread, who summarizes the issue well: the TableModel for JTable is not necessarily meant to be the Model for your problem. It's for Swing's use.

This is essentially why I'm brung to solution 2 or 3.

Can you elaborate why you're not happy with the way you went? Was it painful or tedious to maintain? Did you write notifier methods in your app model?

jdupreza at 2007-7-15 4:46:53 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> Can you elaborate why you're not happy with the way

> you went? Was it painful or tedious to maintain? Did

> you write notifier methods in your app model?

It isn't too bad, and I have two views against the data model so far (and one more in mind). I'm just still not convinced that having to generate meta-data in the view to help interpret the model is quite the right solution.

C'est la vie!

Oh and yes I did write a notifier mechanism for the model.

Yay the ABs.

TimRyanNZa at 2007-7-15 4:46:53 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Thank you for sharing your thoughts.

I think I will go for option 2 (adapter models, both ways).

Option 1 (app Models extending Swing Models), in addition to making me uncomfortable, is a bit awkward to implement as some of my classes would turn out to implement both TreeModel and TableModel.

Option 3 (controller modifying the swing Models after the business logic has updated the app model) is either too coarse-grained (the controller rebuilds the whole TreeModel) or too fine-grained (the controller has to know about each level of the tree hierarchy and update the relevant TreeNode), and I can't find a satisfying middle ground.

I'll probably post my feedback when I have maintained this system for a few months...

J.

P.S.: no harm, I'm more of a soccer fan anyway!

jdupreza at 2007-7-15 4:46:53 > top of Java-index,Other Topics,Patterns & OO Design...