1

I have a JTable and I want to add the column values. I am trying this code:

try{
    double t = 0.00;
    DecimalFormat df = new DecimalFormat("0.00");
    int rows = empTbl.getRowCount();
    for(int row =0; row<=rows; row++) 
    {
        String am = (String) empTbl.getValueAt(row, 7);
        double d = (double) df.parse(am);
        double ans = d+t;
        jLabel7.setText("Total Sale : "+ans);
    }
}
catch(ParseException ex){ }

Why is this code not working?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Pranjal Choladhara
  • 845
  • 2
  • 14
  • 35

1 Answers1

3
  • to try to avoid any parse in Java, there are proper data types

  • add Double / Long / Integer value to the XxxTableModel or JTable directly (depends of unknow code, your issue)

  • change the getColumnClass in XxxTableModel to the Double / Long / Integer

  • add TableModelListener and override setValueAt(proper formula) then to allow formula if value in the JTables Cell changed

  • Double / Float hasn't decimal places (Idiotic thread, with interesting!!! downvoters)

  • use XxxTableCellRenderer for formating output to the Swing GUI

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    +1 for using proper data types. Also, the DecimalFormat should be used by a renderer. See [Table Format Renderers](http://tips4java.wordpress.com/2008/10/11/table-format-renderers/) for an example. – camickr Apr 16 '13 at 14:58