Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Greenhorn
Posts: 23
  • 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.
 
lowercase baba
Posts: 12893
63
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.
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • 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?
 
Marshal
Posts: 25823
69
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: 23
  • 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: 23
  • 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
Marshal
Posts: 25823
69
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).
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crossposted
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic