2011-05-26 65 views
0

我想添加行的JTable這樣的java JTable中,希望的DefaultTableModel addRow

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中只得到第一行,所以我tryied這樣

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){} 

我這種情況下最後一行是不加的。 如何解決這個問題?

編輯 我發現解決方案的價值之一是空字符串這就是爲什麼沒有總和。 它應該像

String base = child.getFirstChildElement("base").getValue(); 
baseSum += Float.parseFloat(base.equals("") ? "0" : base); 
+1

該示例應該爲每個孩子添加一行加總和行。是不是增加了最後一個孩子或總和?你有SSCCE嗎? – jzd 2011-05-26 11:02:39

+0

在secound示例中,不添加SUM,在第一行中添加childs.size()爲7,但在屏幕上只是一行 – 2011-05-26 11:05:26

+0

您在兩段代碼之間做了哪些更改? – jzd 2011-05-26 11:16:44

回答

2

您已使用解決方案編輯了該問題。然而,這個解決方案並不明顯,因爲拋出了一個被忽視和忽視的例外。

這就是爲什麼這行代碼是有問題的一個很好的例子:

catch(Exception e){} 

我建議至少做了e.printStackTrace(),使糟糕的情況下,調試會更容易些。