如何计算JTable中的列总数,但是从特定索引开始

时间:2017-05-06 07:58:19

标签: java swing

我在JTable中有Amount列,有一个'Total'和'Final Total'按钮。因此,如果我将2和3放入金额列并点击'总计',那么我在同一列的另一个单元格中得到5。(我的程序工作正常直到此步骤)现在我想插入5和4以下5.我插入并且点击'Final Total'然后想在最后一行显示相同列(Amount)中的12(5 + 3 + 4)。 (5是我总得到的答案) 在这里,我粘贴了“total”和“Final Total”按钮的代码。我不确定在'Final Total'按钮中使用循环。所以请给出解决方案。

    JButton total = new JButton("Total");
    total.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent ae) 
                {
                    flag=1;
                    add.setEnabled(false);
                    Object source = ae.getSource();
                    if (source == total) 
                    { 
    model.addRow(new Object[]{new Integer(0),"TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
                        for( i = 0; i < model.getRowCount(); i++)
                        {
                             amount = Double.parseDouble(model.getValueAt(i, 6).toString());
                             sum = new Double(sum+amount);
                         }
                        model.setValueAt(sum,model.getRowCount()-1,6);
                         }
                    JOptionPane.showMessageDialog(null,"If you pressed Total then you can't select 'Add Row' button ");
                    model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
                    model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
                    model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
                    model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});

                }
                });


    JButton btnFinalTotal = new JButton("Final total");
    btnFinalTotal.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent ae)
                {
                    flag=2;
                    Object source = ae.getSource();
                    if (source == btnFinalTotal) 
                    {
                    d= Double.parseDouble(model.getValueAt(i,6).toString());
                    System.out.println("Current value of d is  "  +d);

                    model.addRow(new Object[]{new Integer(0),"FINAL TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
                        for(i=0;i>d;i++)
                        {
                            amount = Double.parseDouble(model.getValueAt(i, 6).toString());
                            finalamount= new Double(d+amount);
                        }  
                        model.setValueAt(finalamount,model.getRowCount()-1,6);
                    }
                }
            });

public void tableChanged(TableModelEvent e)
            {

                if (e.getType() == TableModelEvent.UPDATE)
                {
                    System.out.println("Cell " + e.getFirstRow() + ", "
                            + e.getColumn() + " changed. The new value: "
                            + table.getModel().getValueAt(e.getFirstRow(),
                                    e.getColumn()));
                    int row = e.getFirstRow();
                    int column = e.getColumn();
if(flag==1)
                    {
                         if(column == 6)
                         {
                        //   taxonamt= Double.parseDouble(model.getValueAt(row, 6).toString());
                             sum=Double.parseDouble(model.getValueAt(row, 6).toString());
                        //  Double total = new Double (sum*(taxonamt/100));
                             Double total = new Double (sum);
                             model.setValueAt(total,model.getRowCount()-1,7);
                         }
                    }
}
}

0 个答案:

没有答案