tags:

views:

1696

answers:

6

I have a table class that creates modifies a table of items. I want to display those items in a JTable using a table model. To me table model belongs to my gui package but table needs table model in order to fire changes and table model needs table class in order to display it so i can not separate the two. if you need to do this what would be the class structure you use? or do i have a flow in my thinking and they belong in the same package?

A: 

The table model doesn't need to know anything about the table that it's put in. I put the table model in the model package, and the table in the view package. The controller is responsible for asking the model package for the model, asking the view package for the table, and setting that model in the table.

Paul Tomblin
A: 

Sorry i think i missed phrased my question the table class i am talking about isn't a class that's extending JTable its the actual class that holds the data to be viewed in a vector.

+1  A: 

For me the tablemodel is part of the widget, that is the GUI. I wouldn't depend on the javax.swing package in my business logic classes.

So you will use an adapter or other mechanism to load your tablemodel from the business layer, but this degree of separation can be useful.

The decision also depends on the size of the project, and can be a lot more complex than this. E.g. see the jgoodies binding framework and docs for the more abstract cases.

+1  A: 

The model is the bit that holds the data. It should maintain and fire listeners as such models do.

Now you might want some kind of delegation within the model to transform the data or adapt the type. So you have your data in a model object that is not necessarily anything to do with javax.swing.table. Then implement a TableModel that delegates to your underlying model. Create a JTable with that TableModel, but no reference to the underlying model.

Tom Hawtin - tackline
+1  A: 

A TableModel is an adapter. I always write the table model to extends AbstractTableModel and have it as a private inner class of my GUI panel class. You only have to fill in a few methods: getRowCount, getColumnCount and getValueAt

Your GUI class should have access to some controller where it can fill in this information from. When your controller finds out about updates to the data, it should inform your GUI class which can then call AbstractTableModel.fireTableDataChanged or other appropriate methods.

Never use the DefaultTableModel class because you then have to keep the table model's data in sync with the controller's. As I said, the table model is an adapter which presents the controller's data in a way which the JTable can understand

oxbow_lakes
Ahum, you mean the methods that have to be implemented are<pre> public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column);</pre>don't you?
dhiller
Haha yes. Oops! Should have checked that first!
oxbow_lakes
+1  A: 

I strongly suggest that anyone doing significant table or list driven development take a look at Glazed Lists - I've written custom TableModel and CellRenderers for years, binding with JGoodies, etc... I stumbled across GL about a year agin, and my development has never been the same - the Glazed Lists approach is just astounding.

GL has two characteristics that make it my framework of choice for list based data binding and processing:

  1. Very, very easy to do the normal things you want to do
  2. Makes it possible to do the really, really hard things you may want to do

Take a look and see if you aren't impressed. I strongly recommend playing with the sample apps, then watching a few of the screen casts that take you through the actual coding process.

Kevin Day