gpt4 book ai didi

java - 如何计算 JTable 中从特定索引开始的列总数

转载 作者:行者123 更新时间:2023-12-02 02:51:13 25 4
gpt4 key购买 nike

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

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

最佳答案

您需要在数据中放置某种“标记”,将一行指定为“总计”行,以便您可以找到最后一个“总计”行并计算其下​​面的行。

就我个人而言,我会从一个普通的旧 java 对象 (POJO) 开始,它封装了您想要的功能并围绕它构建表模型

关于java - 如何计算 JTable 中从特定索引开始的列总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com