File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Help with AbstractTableModel - Please!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Help with AbstractTableModel - Please!!" Watch "Help with AbstractTableModel - Please!!" New topic

Help with AbstractTableModel - Please!!

Karen Davis

Joined: Mar 04, 2004
Posts: 8
I've included a sample of the code I'm using - Im using a list(rowList) within a list(dataList) and it repeatedly displays the first item in dataList(ie rowList) for the number of rows that should be in the table.
Any help or advice would be greatly appreciated - thanks.

[Andrew: put code inside [code] blocks]
[ March 04, 2004: Message edited by: Andrew Monkhouse ]
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Hi Karen,
The first thing that struck me was that you are using a linked list where and ArrayList would be quicker because it allows for random rather than sequential access. I would personally have only one collection instance variable not three, to hold the row data and perhaps an array to hold the cell data (You know what size a row it will be and it is not unreasonable to think the size wont change in the future often), not three as you have in your implementation.
Perhaps something like the following would make the constructor less complex.

I dont know the exact structure of your project data object classes but that was a guess at it.
You would now have a collection of table rows and can pass this directly to the Tablemodel constructor and could use it as follows
new TableModel(account.getRows());
TableModel(Collection c){
Object getValueAt(col, row){
I hope this helps it is the way I did it but there are probably infinate other ( quite possibly better) ways of doing it
[ March 04, 2004: Message edited by: Mark Smyth ]

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11777

Hi Karen,
Welcome to JavaRanch.
I have edited your post to put the code between [code and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
Sorry that I don't have time to look at your code right now.
Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Just realised I didnt answer your question at all in the previous post, but the reason that the code doesnt work is because rowList is an instance variable.
For the first iteration of the while loop say it adds
Date, Transaction, "Number", "Cr Amount", "Dr Amount
[0]2003/4/56, [1] 2, [2] 123456,[3] null, [4]$5.00
This is added to dataList(0)
At the start of the next iteration you get the next entry.
But you havent cleared the rowList
So you want to add to datalist(1)
[0]2004/4/4, [1] 2, [2] 123423,[3] $10.00, [4]$5.00
But you are actually appending it to the previous entry also and so the list keeps getting bigger for each entry put in.
[0]2003/4/12, [1] 1, [2] 123456,[3] null, [4]$5.00
[5]2004/4/4, [6] 2, [7] 123423,[8] $10.00, [9]$5.00
Now when JTable looks for row data it displays the first 5 elements which are always the same in each row.
Solution is to just make RowList a local variable in the while loop
List rowList=new List();
// Rest of your code
Hope this solves the problem, best of luck with it.
I agree. Here's the link:
subject: Help with AbstractTableModel - Please!!
It's not a secret anymore!