File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Row & Column totals in jTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Row & Column totals in jTable" Watch "Row & Column totals in jTable" New topic
Author

Row & Column totals in jTable

Mike Chilton
Greenhorn

Joined: Jun 18, 2012
Posts: 2

I'm trying to create row and column totals in a jTable. The user can input cell values using 3 text fields (to identify the row, column & value) and a button to add it to the table. The user should then be able to click on a second button which will sum all columns and rows and display the column sums in the last row and the row sums in the last column. Unfortunately I receive an error "NullPointerException" and the program breaks at line 135: salesTotal[row] += Double.valueOf(value).doubleValue();

I am under the impression that the cell values are objects and must be converted to (first) Strings and (second) doubles before you can total them, but not sure how to do this. The program breaks before it can set the value in the appropriate cell, so I haven't been able to test that part yet.

Here is the code.
Hitesh P Patel
Greenhorn

Joined: May 23, 2012
Posts: 8
Which file you are trying to upload ?
If you are talking about code then use code tags to submit your code.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> The user should then be able to click on a second button which will sum all columns and rows...

that's a backward step - use a TableModelListener
Mike Chilton
Greenhorn

Joined: Jun 18, 2012
Posts: 2

Sorry, I wasn't asking for help with design decisions--just how to code row and column totals and put the results into a column and row.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4638
    
    5

Mike Chilton wrote:Sorry, I wasn't asking for help with design decisions

Then I won't advise you that things related to values should be kept in the model, not in the view.

Pity that, seeing as an extended DefaultTableModel with a few overrides provides such a clean solution.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Row & Column totals in jTable