• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sum up the last column of JTable

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have multiple JSpinner. When it is clicked, the name,quantity and price will be added into JTable. I also have one JTextField, used to sum up the total price, which is in the last column of the table.

To get the total of the last column of each row, I use this code

Example (Correct)

When I change the qty from 2 to 1, it sum up instead of minus
 
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the variable total declared?
It looks like it's not local to the method, so the value is retained between calls to stateChanged.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Where is the variable total declared?
It looks like it's not local to the method, so the value is retained between calls to stateChanged.


I declared it as global
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do you initialize total to zero?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Where do you initialize total to zero?


I declared it as global.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to solve ?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, think it through.
At the moment you have a "global" total value and, when the user changes a number in the table, you then add the new value for that row to this total.

So in your example, your total value was 17, the user changes the value in one row so it now says 6.  Your code adds that 6 to the total...so it's now 23.

So what should be done to ensure the total is calculated correctly?
 
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't keep a "total" variable.

Instead, whenever any of the values in your table change that will affect the total, simply iterate through all the rows in the table to recalculate the total.

This way you don't need to how the values change. That is if the quantitly goes from 2 to 3, or 2, to 1, the logic is still the same, you don't need to worry about calculating the difference from the previous value.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Don't keep a "total" variable.
simply iterate through all the rows in the table to recalculate the total.

Can you explain more on this ?
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you already have looping code, but you only ever process one row in the loop.

I am suggesting you get rid of the if condition and recalculate the total as you loop through each row. So before the loop starts you set the total value to 0, then for each row you calculate the row amount and add it to the total. When the loop finishes you then display the total value.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me if I am doing wrong. I getting java.lang.NumberFormatException error

Error


 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't debug a NumberFormatException.

Only you have the data. Your data is not a number.

Add some System.out.println(...) statements to see what the data is that you are actually trying to convert to a number. Or use a debugger to step through the code to see what your data actually is.

This is basic debugging.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Qty is the title, which is in the first row of the JTable.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a title string in the first row of the table? Why not just use column headers, which aren't part of the table?

But anyway if you're going to have the first row of the table be text strings describing the rest of the rows, then clearly you wouldn't bother trying to include the first row when summing up the rest of the rows, which is where the data actually is.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should I get rid of the first row of JTable in order to get the total value ?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the latest code I tried

By using above code, I get output qty and price. If I change row to rows in this two lines,
I get error

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted at http://www.dreamincode.net/forums/topic/406165-sum-up-the-total-value-in-jtable/
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't belive I've been stucked at this part almost one week !
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 3 you set "rows" to the row count. One line 5 you loop while row < rows. On line 8 you decrement the number of rows but that is not reflected in your "rows" variable.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:How should I get rid of the first row of JTable in order to get the total value ?



That's entirely the wrong way to look at it. Don't get rid of the first row, just don't include it in the rows which you're adding up. There isn't an iron law which says that the index of a Java for-loop must start at zero. Write your loop so that it only covers the rows you want to add up.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange...I trying to debug my application and getting weird result.

 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I click the JSpinner first time(1), I get in else . Fine
When I click the JSpinner second time(2), I get
in else
in else
inside if statement


Why I would not getting inside if else ? Is it because of the return ? But if I remove the return, it will keeps on adding items in new rows
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, is this ever an example of why bad formatting is a bug.  Here's your code formatted correctly[1]

Does the SOP line that says "in if else" make any sense now?

[1] There are different styles to format with -- this isn't the only one.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic