java JTable, defaultTableModel want addRow
I'm trying to add row to JTable like this
DefaultTableModel model = new DefaultTableModel();
try {
Builder builder = new Builder();
Document doc = builder.build(Config.PATH +"incasation.xml");
Element root = doc.getRootElement();
Elements childs = root.getChildElements("locations");
model.addColumn("Name");
model.addColumn("Total");
model.addColumn("Location fee");
model.addColumn("Bank");
model.addColumn("Tax");
float baseSum = 0;
float locationSum = 0;
float bankSum = 0;
float taxSum = 0;
for(int i=0; i< childs.size(); i++)
{
Element child = childs.get(i);
model.addRow(new Object[] {
child.getFirstChildElement("name").getValue(),
child.getFirstChildElement("base").getValue(),
child.getFirstChildElement("locationfee").getValue(),
child.getFirstChildElement("bank").getValue(),
child.getFirstChildElement("tax").getValue()
});
baseSum += Float.parseFloat(child.getFirstChildElement("base").getValue());
locationSum += Float.parseFloat(child.getFirstChildElement("locationfee").getValue());
bankSum += Float.parseFloat(child.getFirstChildElement("bank").getValue());
taxSum += Float.parseFloat(child.getFirstChildElement("tax").getValue());
}
model.addRow(new Object[] {
"SUM",
Float.toString(baseSum),
Float.toString(locationSum),
Float.toString(bankSum),
Float.toString(taxSum)
});
}
catch(Exception e){}
and in that case it JTable gets only first row, so I tryied like this
DefaultTableModel model = new DefaultTableModel();
try {
Builder builder = new Builder();
Document doc = builder.build(Config.PATH +"incasation.xml");
Element root = doc.getRootElement();
Elements childs = root.getChildElements("locations");
model.addColumn("Name");
model.addColumn("Total");
model.addColumn("Location fee");
model.addColumn("Bank");
model.addColumn("Tax");
float baseSum = 0;
float locationSum = 0;
float bankSum = 0;
float taxSum = 0;
for(int i=0; i< childs.size(); i++)
{
Element child = childs.get(i);
model.addRow(new Object[] {
child.getFirstChildElement("name").getValue(),
child.getFirstChildElement("base").getValue(),
child.getFirstChildElement("locationfee").getValue(),
child.getFirstChildElement("bank").getValue(),
child.getFirstChildElement("tax").getValue()
});
}
for(int j=0; j< childs.size(); j++)
{
Element child = childs.get(j);
baseSum += Float.parseFloat(child.getFirstChildElement("base").getValue());
locationSum += Float.parseFloat(child.getFirstChildElement("locationfee").getValue());
bankSum += Float.parseFloat(child.getFirstChildElement("bank").getValue());
taxSum 开发者_如何学Go+= Float.parseFloat(child.getFirstChildElement("tax").getValue());
}
model.addRow(new Object[] {
"SUM",
Float.toString(baseSum),
Float.toString(locationSum),
Float.toString(bankSum),
Float.toString(taxSum)
});
}
catch(Exception e){}
I this case the last row is not added. How to solve this problem?
EDIT I found the solution one of the value was empty string thats why there was no sum. It should be like
String base = child.getFirstChildElement("base").getValue();
baseSum += Float.parseFloat(base.equals("") ? "0" : base);
You have edited the question with a solution. However, the solution was not obvious because an exception was thrown that was caught and disregarded.
This is a great example of why this line of code can be problematic:
catch(Exception e){}
I would suggest to at least do a e.printStackTrace()
, so that worse case debugging would be easier.
精彩评论