0

I have a Column called "Salary" in jTable whose data type is set to int which gets its data(say n number of records) from a database in MYSQL through connectivity.

I want to add all the salaries present in 'n' number of rows in the column salary and display it as total money spent by the company.

I thought about using getText(Integer.parseInt()); method to get the values,store it in data type int, sum it up and display it in a jTextField but the number of rows/data records is not fixed.

I've tried the following

int total =0;
        for (int i = 0; i < jTable1.getRowCount(); i++)
            {
                int num = (int) jTable1.getValueAt(i,3); //3rd column, i number of rows
                total = total  +num;
            }
        total1.setText(Integer.toString(total));

EDIT: error when running the code

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
    at java.util.Vector.elementAt(Vector.java:474)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
    at sun.swing.SwingUtilities2.convertColumnIndexToModel(SwingUtilities2.java:1988)
    at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2582)
    at javax.swing.JTable.getValueAt(JTable.java:2718)
    at cart.<init>(cart.java:91)
    at cart$3.run(cart.java:264)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Aurorachilles
  • 83
  • 1
  • 1
  • 8

0 Answers0