Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AbstractTableModel question...

2 views
Skip to first unread message

IchBin

unread,
Mar 30, 2006, 2:59:26 AM3/30/06
to

I have always used the DefaultTableModel for JTables, no problem. It
seems that I have to use TableModel and AbstractTableModel because I
need to do some table sorting. I have it all setup having my TableModel
loaded into the AbstractTableModel sorting model which is loaded into my
jTable.. Naturally I have insert my data into my model. Problem is that
TableModel is not defined to load data, other than at a row\column.

Question, do I just added a new method to do the add of of row to my
TableModel. And if so I guess that I would have to at the same time
issue a fireTableDataChanged() to complete the updated process. I will
be using seven table ala tabs that I need to keep the TableModels
separate which should be no problem.

It would be fine if I new what my data and columns was going to be and
have it defined, to the Model, before hand but I don't know what the
data will be. All I see in any examples are not really good examples of
a normal table use.

Sorry, I'm tied: a long day. I would have not problem using Mustang to
do what I need to do but then that would run me into distribution problems.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Nigel Wade

unread,
Mar 30, 2006, 3:26:22 AM3/30/06
to
IchBin wrote:

>
> I have always used the DefaultTableModel for JTables, no problem. It
> seems that I have to use TableModel and AbstractTableModel because I
> need to do some table sorting. I have it all setup having my TableModel
> loaded into the AbstractTableModel sorting model which is loaded into my
> jTable.. Naturally I have insert my data into my model. Problem is that
> TableModel is not defined to load data, other than at a row\column.
>

Have you looked at the TableSorter class which Sun provide as an example in the
Swing Tutorial?
http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/TableSorter.java

This extends DefaultTableModel, and may do most of what you want. It might be
easier to start with this class and modify to suit your needs. When I needed a
sorted table that was what I did.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : n...@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555

IchBin

unread,
Mar 30, 2006, 9:13:11 AM3/30/06
to
Nigel Wade wrote:
> IchBin wrote:
>
>> I have always used the DefaultTableModel for JTables, no problem. It
>> seems that I have to use TableModel and AbstractTableModel because I
>> need to do some table sorting. I have it all setup having my TableModel
>> loaded into the AbstractTableModel sorting model which is loaded into my
>> jTable.. Naturally I have insert my data into my model. Problem is that
>> TableModel is not defined to load data, other than at a row\column.
>>
>
> Have you looked at the TableSorter class which Sun provide as an example in the
> Swing Tutorial?
> http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/TableSorter.java
>
> This extends DefaultTableModel, and may do most of what you want. It might be
> easier to start with this class and modify to suit your needs. When I needed a
> sorted table that was what I did.
>

Thanks Nigel.. Last night I had finally settled on using the TableSorter
because it implements the decorator pattern. I was also impressed by
what the TableSorter class does. The problem, for me, is that it
implements this pattern using the TableModel. Also the TableSorter
extends the AbstractTableModel not the DefaultTableModel.

Here lies my dilemma. If you used this Model how did you to say.. add
data, via calling program, to your data Model or say even columns? The
data model has to extend TableModel. Maybe I am wrong if you are using it?

Judy Szikora

unread,
Mar 30, 2006, 11:22:45 AM3/30/06
to
IchBin wrote:
>
> Here lies my dilemma. If you used this Model how did you to say.. add
> data, via calling program, to your data Model or say even columns? The
> data model has to extend TableModel. Maybe I am wrong if you are using it?
>

AbstractTableModel has a bunch of fire methods, two of which are
fireTableStructureChanged and fireTableDataChanged. Is that what you're
missing?

--
Judy Szikora, Apprisant Technologies Inc.
http://www.apprisant.com

IchBin

unread,
Mar 30, 2006, 2:03:18 PM3/30/06
to
Judy Szikora wrote:
> IchBin wrote:
>>
>> Here lies my dilemma. If you used this Model how did you to say.. add
>> data, via calling program, to your data Model or say even columns? The
>> data model has to extend TableModel. Maybe I am wrong if you are using
>> it?
>>
>
> AbstractTableModel has a bunch of fire methods, two of which are
> fireTableStructureChanged and fireTableDataChanged. Is that what you're
> missing?
>
No.. Not sure how to added my data\columns to my data model if it has to
extend TableModel or AbstractTableModel using a decorator pattern. They
only supply a insert by row/column and not row. Not sure if I just add
my own code to do this. They has to be a lot of people doing this. I
just have not had the occasion to have to write code to support table
sorting. I have all of things I need as I mentioned in the original
post. just not what I mentioned in the above.

Daniel

unread,
Mar 31, 2006, 1:31:20 AM3/31/06
to
Whenever I have had reason to use JTables I have always extended the
AbstractTableModel. Like you say it only supports insertion by row or
column. That is not always what you are after. But the main issue here
is that the abstracttablemodel does not hold any data. THAT is up to
you to fix. So in your class you need to hold the data, or provide
some means of getting the data via the getObject(int row,int col)
method. This of course gives you pretty much free hands on how you
want to update the contents. Not knowing specifically what you need to
do, but I think adding a method that does the job is exactly what you
want. Just remeber what the precious poster said, you must inform the
table that your data has changed via some event firing. THAT is what
the abstract tablemodel does for you. So you simply call either the
fireTableDataChanged() or the fireTableStructureChanged() after you
have all your new data. The table won't be uppdated unless it is
informed of the change. The methods you have seen is to inform the
table that we have inserted a new column or row. Probably via our own
update method.
So in short, just write your own method for updating the tablemodel.
At the end of that method call either of the fireTableDataChanged or
fireTableStructureChanged.

regards
Daniel

IchBin

unread,
Mar 30, 2006, 6:17:32 PM3/30/06
to

Thanks Dan.. that is what I am doing now as it turns out.

I always use the DefaultTableModel and never have any problems. So
really the Java Tutorial example for table sorting is not really a
workable example because most people do not hard code their columns and
data in their TableModel. OK.

I can not wait until 1.6 with it's new filtering and sorting mechanism.

Nigel Wade

unread,
Apr 3, 2006, 5:44:16 AM4/3/06
to
IchBin wrote:

Basically, I changed it so it extended DefaultTableModel rather than
AbstractTableModel.

Nigel Wade

unread,
Apr 3, 2006, 6:27:34 AM4/3/06
to
Nigel Wade wrote:

Sorry, hit send by mistake...

It is necessary to override the addRow/insertRow/removeRow/setRowCount methods
of DefaultTableModel as there is some additional work to be done. One caveat of
adding/inserting/removing rows is that the sorting status needs to be reset.
There's also the added ambiguity with insert/remove as to which row number is
meant - the unsorted index or the sorted index. The same also applies to
getValueAt and setValueAt, the methods in TableSorter apply to the sorted index
which might not be what you want. I added the methods getUnsortedValueAt and
setUnsortedValueAt to work with the unsorted model data.

It's quite a lot of fun...

IchBin

unread,
Apr 3, 2006, 11:59:22 AM4/3/06
to
Thanks Nigel..

I based on your prior thread message I used the TableModel and just
added methods I needed: addRow(), addColumn() and methods to manage the
String[]columnNames and Object[][] data Objects.

I guess I really should not have done it this way because it does not
conform to the JComponent's MVC implementation.

I put it down a few days ago but I will revisit and convert it to use
the DefaultTableModel.

0 new messages