• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Calculate sum in jtable and display the sum in jtextfield

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,i got a table that contains codes,names,qty,price and totals.What i want to ask is how to calculate the qty*price and the total was putted in the the totals column and also putted in the JTextField..When there are new rows,or deleted rows,it will automatically edited in the JTextField..Thanks a lot.
 
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add a TableModelListener to your JTable's model to listen for add / delete / change events. For the rest it's just iterating over your model:

That loop body is pseudo code of course; you need to fill in X and Y to be your quantity and price columns, cast the the results of the getValueAt calls to whatever class you are storing, and retrieving the primitive from them. That part is quite trivial though.

May I add one suggestion though: float and double are not precise enough for money calculations. java.math.BigDecimal can be used instead. The loop body would become:

You could use Integer or Long for the quantity if you only want whole units, but then you would need to convert when calculating the total.
 
yesaya handoyo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,thanks a lot..I'll try it..Anyway how to get the value for filling the x and y??Thanks..
 
Rob Spoor
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you've specified those when creating your TableModel.
Suppose you've created it with headers "Column 1", "Column 2", "Price", "Column 4", "Quantity" and "Column 6", then X and Y are 2 and 4 (since indexing starts at 0).
 
yesaya handoyo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,i get it..I'll try it.Thanks a lot..
 
yesaya handoyo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to ask again.How about display the total into the textfield?Suppose i delete a row in the jtable?What should i do to change the sum in the textfield??Thanks a lot..
 
Rob Spoor
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go through the API, you would find that JTextField has a method called setText.
If you delete a row, just calculate the total from the new contents again. Ideally you would take the total and subtract the value for the row, but once it is deleted you loose that entire row before the event is triggered.

Come to think of it, I think you can do this without TableModelListener if you create your own TableModel:

I may have missed a few methods that can change your table data, but you probably get the idea.

Also note how I only alter total after the call to the parent's method implementation (with super). If for some reason this method throws an exception your total will still be valid.


With this table model your total will always be up-to-date, and you can just retrieve it through a getter method.
[ October 17, 2008: Message edited by: Rob Prime ]
 
yesaya handoyo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,i'll try it out.Suppose using netbeans is no a big difference,isn't it?Thanks..
 
Rob Spoor
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if Netbeans sets a default table model or anything, but you can always override that manually after the Netbeans generated code.
 
yesaya handoyo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,thanks Rob.May God bless you for your help..
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ambika,
I am sorry but I had to edit your post. What you had done (posting your question in someone else's post) is called hijacking the thread!
More details here http://faq.javaranch.com/java/UseOneThreadPerQuestion

You can always start a new thread for your question. Also please remember to use code tags
http://faq.javaranch.com/java/UseCodeTags
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic