• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and getValueAt(int, int)

 
Stuart Hoffman
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble trying to sum values accross the rows in a JTable.
I extended DefaultTableModel for my tableModel then returned a ResultSet via SQLJ to populate the table. The data displays when I run the Applet and I have overridden isCellEditable making cells 1 to 8 editable. Structure of the table is as follows:
ID, Project, hrsMon hrsSun,.................................total hrs
int, String, float, float, float, float, float, float, float, float
The first(0) and last(9) columns are not editable. The last column(9) a float is the total. So when a user puts a value in 2-8 and presses enter
the total should be calculated anew.
currently I have something like this:
public Object getValueAt(Float newVal,int row, int col)
{
BigDecimal sum = new BigDecimal(1);
if(col == 9) // Total column
{
try
{

//sum = new BigDecimal(Float.parseFloat((String)getValueAt(row, 2)) + Float.parseFloat((String)getValueAt(row, 3))
// + Float.parseFloat((String)getValueAt(row, 4)) + Float.parseFloat((String)getValueAt(row, 5))
// + Float.parseFloat((String)getValueAt(row, 6)) + Float.parseFloat((String)getValueAt(row, 7))
// + Float.parseFloat((String)getValueAt(row, 8)));


//sum = BigDecimal.valueOf(Long.parseLong((String)getValueAt(row,2)));
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
// sum = new BigDecimal(0);
}

setValueAt(sum,row,9);

return sum;
}
else
{
return super.getValueAt(row, col);
}

}

-----
I finally had to comment out the calculation because i could not get it to compile. I've changed it so many times that I cannot get the code to work. I don't know if BigDecimal is what I want to return or maybe Object or Float? Float makes sense to my newbie mind as that is the type of the field in the database.
Any help is greatly appreciated.
Thanks
Stuart
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic