By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,792 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,792 IT Pros & Developers. It's quick & easy.

Calculating Total Value in JTable Column Using New Value

irwansyahazniel
P: 2
I want to calculate total value in index [0][2], previous value is 0, but I have replaced and display it with the calculation result 1/y, and when I calculate total value, value that summed is previous value that containing 0, this my code,

Expand|Select|Wrap|Line Numbers
  1.  
  2. //observation table
  3.                                          //0             1        2    3      4      5     6                       
  4.          titleColumn = new Object[]{"Time (Second)","Medicine", "1/y","x2", "X/Y", "Y^", "Error"};
  5.                                    //0   1    2   3   4   5   6
  6.         allData = new Double[][]  {{1.0,1.02,0.0,0.0,0.0,0.0,0.0},
  7.                                    {2.0,0.667,0.0,0.0,0.0,0.0,0.0},
  8.                                    {3.0,0.367,0.0,0.0,0.0,0.0,0.0},
  9.                                    {4.0,0.278,0.0,0.0,0.0,0.0,0.0},
  10.                                    {5.0,0.237,0.0,0.0,0.0,0.0,0.0},
  11.                                    {6.0,0.187,0.0,0.0,0.0,0.0,0.0},
  12.                                    {7.0,0.155,0.0,0.0,0.0,0.0,0.0},
  13.                                    {8.0,0.156,0.0,0.0,0.0,0.0,0.0},
  14.                                    {9.0,0.142,0.0,0.0,0.0,0.0,0.0},
  15.                                    {10.0,0.111,0.0,0.0,0.0,0.0,0.0},
  16.                                    {11.0,0.12,0.0,0.0,0.0,0.0,0.0},
  17.                                    {12.0,0.097,0.0,0.0,0.0,0.0,0.0},
  18.                                    {13.0,0.099,0.0,0.0,0.0,0.0,0.0},
  19.                                    {14.0,0.089,0.0,0.0,0.0,0.0,0.0},
  20.                                    {15.0,0.079,0.0,0.0,0.0,0.0,0.0},
  21.                                    {0.0,0.0,0.0,0.0,0.0,0.0,0.0}};
  22.  
  23.         tableModelObservation = new DefaultTableModel(allData, titleColumn);
  24.         tableObservation.setModel(tableModelObservation);
  25.         int row,column,inputRow,inputColumn;
  26.  
  27.         //index [0][2] was replaced with calculation 1/y
  28.         row = 0;
  29.         column = 1;
  30.         inputRow = 0;
  31.         inputColumn = 2;
  32.         double onePerY = 0;
  33.         for(int a=0;a<allData.length;a++){
  34.             onePerY = 1/allData[row][column];
  35. tableObservation.getModel().setValueAt(onePerY,  inputRow, inputColumn);  
  36.             inputRow++;
  37.             row++;
  38.         }    
  39.  
  40.         //calculation total 1/y, summing is still using previous value "0"
  41.         row = 0;
  42.         column = 2;
  43.         inputRow = 15;
  44.         inputColumn = 2;
  45.         double totalOnePerY = 0;
  46.         for (int a=0;a<allData.length;a++){
  47.             totalOnePerY += allData[row][column];
  48.             row++;
  49.         }
  50.         //displaying result in row index[15] and column index[2] 
  51.         tableObservation.getModel().setValueAt(totalOnePerY, inputRow, inputColumn);
  52.  
this column values 1/y after calculation process



What should I do, to be able to summing it using new value ?
all the assistance that you gave, I would appreciate it, thank you


[1]: http://i.stack.imgur.com/wTYFt.png
Jan 18 '13 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,422
You set the value in tableModelObservation but you're adding up the values in allData. They are not the same.
Jan 18 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.