aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Calculate sum in jtable and display the sum in jtextfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Calculate sum in jtable and display the sum in jtextfield" Watch "Calculate sum in jtable and display the sum in jtextfield" New topic
Author

Calculate sum in jtable and display the sum in jtextfield

yesaya handoyo
Greenhorn

Joined: Aug 31, 2008
Posts: 25
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
yesaya handoyo
Greenhorn

Joined: Aug 31, 2008
Posts: 25
Ok,thanks a lot..I'll try it..Anyway how to get the value for filling the x and y??Thanks..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Aug 31, 2008
Posts: 25
Ok,i get it..I'll try it.Thanks a lot..
yesaya handoyo
Greenhorn

Joined: Aug 31, 2008
Posts: 25
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Aug 31, 2008
Posts: 25
Ok,i'll try it out.Suppose using netbeans is no a big difference,isn't it?Thanks..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Aug 31, 2008
Posts: 25
Ok,thanks Rob.May God bless you for your help..
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10247
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate sum in jtable and display the sum in jtextfield