• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't sum the total of each row in a JTable  RSS feed

 
Johnny Dey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable that implements abstract table model. there are 4 columns and 30 rows with Integer objects in it. I would like to use the last cell of each row to hold the sum of each row and it should change dynamically during programs runtime i.e. when users change the cell value the the total at end of the row should change. In my table model's getValueAt() method, I have the following code



When I run this code the whole program crashes and every cell gets jumbled with too many numbers. Can anyone tell me what's wrong in my code. Thanks in advance.

EDIT: well there is no error message in the console but the JTable's cell data gets bigger and bigger along with the scroll bar. for example if a cell has a number 1 in it, more numbers are added like it was 1 first then 1111111111111111......and keeps growing until the program stops responding.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12441
42
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by "The whole program crashes"? Usually you get some kind of error message, with lots of useful information. Simply saying "It doesn't work" doesn't let us help you.
 
K. Tsang
Bartender
Posts: 3631
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should comment out that sum column for now.

You first need to put the values in the correct cells. Are you using a java bean type object for your row data? How are you moving from one cell to the next?

How is your table model look like?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Dey wrote:I would like to use the last cell of each row to hold the sum of each row


Shouldn't this read "I would like to use the last cell of each row to hold the sum of all cells in the row except the last cell? Your code's random indentation doesn't help me to understand the code, and neither does the use of ".length" sometimes and "30" sometimes, but I suspect you may not be doing exactly that.
 
Johnny Dey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't this read "I would like to use the last cell of each row to hold the sum of all cells in the row except the last cell?

yeah it should, I am sorry I couldnt phrase the question well.

Your code's random indentation doesn't help me to understand the code

I am new to this forum and I dont have much about post formatting and indenting. I have pasted the code as indented in eclipse but this website is not showing the indentation properly. I may be wrong though.

and neither does the use of ".length" sometimes and "30" sometimes


well, I changed the code and posted the table model. I hope it makes sense now.
 
Johnny Dey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:I think you should comment out that sum column for now.

You first need to put the values in the correct cells. Are you using a java bean type object for your row data? How are you moving from one cell to the next?

How is your table model look like?


I am using eclipse and extended my JTable to Abstract Table Model. I posted the table model.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why your underlying data structure is an Object[][] when you only put integer values into it. That just forces you into casting all over the place.

And as for your calculation: you calculate "sum" as the sum of all the columns in the row, including the last one which is supposed to contain the sum. But then you don't do anything with that calculation.

So here's what I would do:

1. Fix isCellEditable so that it doesn't allow the user to edit the last column.

2. Fix getValueAt so that it returns the value from the array for all columns except the last, and so that it returns the sum of the row for the last column. Don't bother storing this value.

3. Fix setValueAt so that you fireTableCellUpdated for the total column as well as the column which was changed (since the total will have changed also).
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crossposted
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!