javaswingjtabledocumentlistener

JTable erroneous column summation values


Scenario :
A JTable contains the following data, I have tried to describe what I am trying to do in the image below :- enter image description here

So, I guess I am able to explain what I am trying to achieve here.

Problem faced Does not display the accurate results (summation), ofcourse. The code I have used is :

public void docTotal_Income(){
   try{

       int totC=8,xC=3,lC=4,eC=5, sC=6; // totC is the last column, xC-3rd, lC-4th and so on...


       for(int i=0;i<(easypath.doctorBusiness_table.getRowCount());i++){ // "easypath.doctorBusiness_table" is the table name
           sumTot += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, totC).toString());
           sumTotx += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, xC).toString());
           sumTotl += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, lC).toString());
           sumTote += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, eC).toString());
           sumTots += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, sC).toString());
       }
       easypath.totalEarnt_docBus_tf.setText(String.valueOf(sumTot));
       easypath.xTotIncome_tf.setText(String.valueOf(sumTotx));
       easypath.lTotIncome_tf.setText(String.valueOf(sumTotl));
       easypath.eTotIncome_tf.setText(String.valueOf(sumTote));
       easypath.sTotIncome_tf.setText(String.valueOf(sumTots));

       sumTot = 0;   // public static    
       sumTotx = 0;  // values globally
       sumTotl = 0;  // declared
       sumTote = 0;  // and
       sumTots = 0;  // initialised 0 

       }
       catch(Exception ex){
           ex.printStackTrace();
           JOptionPane.showMessageDialog(null, "Error in totalling income");
       }
   }

I am calling the method docTotal_Income() after refining the JTable with the Document Listener (works fine) and finally firing an eventListener on the JButton.

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new doctor().docTotal_Income(); // doctor is the class
}

After all these I get irregular summations. I am guessing I have gone wrong somewhere with logic, but is there any other thing I am missing?

I would gladly appreciate any suggestion on this. Thanks for your time


Solution

  • new doctor().docTotal_Income(); // doctor is the class
    

    First of all class names should start with an upper case character. "doctor()" should be "Doctor()".

    Why are you creating a new Doctor()?

    If you are trying to filter the data in the TableModel then you need to get the data from the table, not the TableModel.

    So your code should be something like:

    JTable table = easypath.doctorBusiness_table;
    
    for(int i=0; I < table.getRowCount(); i++)
    {
           sumTot += Double.parseDouble(table.getValueAt(i, totC).toString());
           sumTotx += Double.parseDouble(table.getValueAt(i, xC).toString());
           sumTotl += Double.parseDouble(table.getValueAt(i, lC).toString());
           sumTote += Double.parseDouble(table.getValueAt(i, eC).toString());
           sumTots += Double.parseDouble(table.getValueAt(i, sC).toString());
    }