我正试图像这样在JTable中添加行
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){}在这种情况下,JTable只得到第一行,所以我尝试这样做
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 += 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在这种情况下,没有添加最后一行。如何解决这个问题?
编辑我找到了解决方案之一的值是空字符串,这就是为什么没有和。应该就像
String base = child.getFirstChildElement("base").getValue();
baseSum += Float.parseFloat(base.equals("") ? "0" : base); 发布于 2011-05-26 12:22:42
你已经用一个解决方案编辑了这个问题。但是,解决方案并不明显,因为抛出的异常被捕获和忽略。
这是一个很好的例子,说明了为什么这一行代码会有问题:
catch(Exception e){}我建议至少做一个e.printStackTrace(),这样更容易进行更坏的情况调试。
https://stackoverflow.com/questions/6137361
复制相似问题