首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java JTable,defaultTableModel want

java JTable,defaultTableModel want
EN

Stack Overflow用户
提问于 2011-05-26 10:50:50
回答 1查看 2.7K关注 0票数 0

我正试图像这样在JTable中添加行

代码语言:javascript
复制
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只得到第一行,所以我尝试这样做

代码语言:javascript
复制
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在这种情况下,没有添加最后一行。如何解决这个问题?

编辑我找到了解决方案之一的值是空字符串,这就是为什么没有和。应该就像

代码语言:javascript
复制
String base = child.getFirstChildElement("base").getValue();
baseSum += Float.parseFloat(base.equals("") ? "0" : base);  
EN

回答 1

Stack Overflow用户

发布于 2011-05-26 12:22:42

你已经用一个解决方案编辑了这个问题。但是,解决方案并不明显,因为抛出的异常被捕获和忽略。

这是一个很好的例子,说明了为什么这一行代码会有问题:

代码语言:javascript
复制
catch(Exception e){}

我建议至少做一个e.printStackTrace(),这样更容易进行更坏的情况调试。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6137361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档