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.
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.
posted 6 years ago
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.
posted 6 years ago
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.
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).