如何总结一个jtable的列

时间:2015-02-11 08:21:48

标签: java swing netbeans jtable sum

我想总结一下我的jTable列。 我已经阅读了很多其他论坛条目,但它不适合我的问题或它适合但后来发生了一个新问题... 我的代码如下:

 private void berechnenbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                

     int prow=0;

     while(prow<=ptable.getRowCount()){
        double d = Double.parseDouble(ptable.getValueAt(prow, 1)+""); 
        int Amount = (int) d;
        prow++;
     }
}

这就是错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at javax.swing.JTable.getValueAt(JTable.java:2652)
at gedanke.Leckageberechnung_2.berechnenbuttonActionPerformed(Leckageberechnung_2.java:491)
at gedanke.Leckageberechnung_2.access$800(Leckageberechnung_2.java:15)
at gedanke.Leckageberechnung_2$11.actionPerformed(Leckageberechnung_2.java:175)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

我也试过这个:

phoehegesamt+= (Integer) ptable.getValueAt(prow, 1);

和这一个:

phoehegesamt+=((integer) ptable.getValueAt(prow,1)).intvalue(); 

但两人都以此错误结束:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer 
at gedanke.Leckageberechnung_2.berechnenbuttonActionPerformed(Leckageberechnung_2.java:491) 
at gedanke.Leckageberechnung_2.access$800(Leckageberechnung_2.java:15) 
at gedanke.Leckageberechnung_2$11.actionPerformed(Leckageberechnung_2.java:175) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
at java.awt.Component.processMouseEvent(Component.java:6038) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260) 
at java.awt.Component.processEvent(Component.java:5803) 
at java.awt.Container.processEvent(Container.java:2058) 
at java.awt.Component.dispatchEventImpl(Component.java:4410) 
at java.awt.Container.dispatchEventImpl(Container.java:2116) 
at java.awt.Component.dispatchEvent(Component.java:4240) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
at java.awt.Container.dispatchEventImpl(Container.java:2102) 
at java.awt.Window.dispatchEventImpl(Window.java:2429) 
at java.awt.Component.dispatchEvent(Component.java:4240) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

我感谢任何帮助! :) 斯特芬

1 个答案:

答案 0 :(得分:0)

对于表的值总和,列必须递增。你递增了行.. 希望这会有所帮助。

        double sum = 0;
        int prow=1;

        DefaultTableModel t = (DefaultTableModel) ptable.getModel();
        for (int i = 0; i < t.getRowCount(); i++) {
            sum = sum + Double.parseDouble(t.getValueAt(prow, i).toString());

        }