gpt4 book ai didi

java - 从 JTable 添加/删除行后如何添加和减去总和值

转载 作者:行者123 更新时间:2023-12-01 17:30:05 25 4
gpt4 key购买 nike

我有为 JTable 添加和删除单行的代码,但它会导致问题,例如从 JTable 中删除后无法提供总和和减法的准确计算>。 JTable 充当“购物车”,用于添加和删除购物车中的项目。

用于将行添加到购物车的添加到购物车按钮:

if (e.getSource() == movebutton) {
TableModel model1 = productTable.getModel();
int index[] = productTable.getSelectedRows();
Object[] row = new Object[4];
DefaultTableModel model2 = (DefaultTableModel) cartTable.getModel();

for (int i = 0; i < index.length; i++) {
row[0] = model1.getValueAt(index[i], 0);
row[1] = model1.getValueAt(index[i], 1);
row[2] = model1.getValueAt(index[i], 2);
model2.addRow(row);
getSum();
}

添加整体项目的代码:

public void getSum() {  //column 02 is where the item's prices are listed
for (int i = 0; i < cartTable.getRowCount(); i++) {
total += Double.parseDouble(cartTable.getValueAt(i, 2).toString());
}
cartAmount.setText("Total: P " + Double.toString(total));
}

}

从购物车中删除商品按钮代码:

 try {
for (int i = 0; i < cartTable.getRowCount(); i++) {
total = total - Double.parseDouble(cartTable.getValueAt(i, 2).toString());
}
cartAmount.setText("Total: P " + Double.toString(total));
if (total <= 0.0) {
total = 0.0;
}
{
int getSelectedRowForDeletion = cartTable.getSelectedRow();
model2.removeRow(getSelectedRowForDeletion);
JOptionPane.showMessageDialog(null, "Item removed from cart");
}
} catch (NumberFormatException ex) {
ex.printStackTrace();
} catch (ArrayIndexOutOfBoundsException ex) {
}
}

以下是添加和删除项目时的一些图片: enter image description here

enter image description here

enter image description here

当未选择任何行时,如何才能使删除按钮不起作用?就像要求用户在删除之前选择一行?还消除了负和计算的可能性。谢谢

最佳答案

我解决了:

添加到购物车按钮:

 if (e.getSource() == movebutton) {
try {
int index[] = productTable.getSelectedRows();
Object[] row = new Object[4];

for (int i = 0; i < index.length; i++) {
row[0] = model.getValueAt(index[i], 0);
row[1] = model.getValueAt(index[i], 1);
row[2] = model.getValueAt(index[i], 2);
model2.addRow(row);
}
DefaultTableModel t = (DefaultTableModel) productTable.getModel();
int selectedRow = productTable.getSelectedRow();
{
total += Double.parseDouble(t.getValueAt(selectedRow, 2).toString());
}
cartAmount.setText("Total: P " + Double.toString(total));
}catch (ArrayIndexOutOfBoundsException a) {
}
}

删除按钮:

if (e.getSource() == remove) {
try {
DefaultTableModel t = (DefaultTableModel) cartTable.getModel();
int getSelectedRowForDeletion = cartTable.getSelectedRow();
if (getSelectedRowForDeletion >= 0) {
int selectedRow = cartTable.getSelectedRow();
total -= Double.parseDouble(t.getValueAt(selectedRow, 2).toString());
cartAmount.setText("Total: P " + Double.toString(total));
model2.removeRow(getSelectedRowForDeletion);
JOptionPane.showMessageDialog(null, "Item removed from cart!");
} else {
JOptionPane.showMessageDialog(null, "Select an item to remove.");
}
} catch (ArrayIndexOutOfBoundsException a) {
}
}

我在添加值时删除了 for 循环,因为它只会添加 ProductTable 中的下一个项目。删除按钮也是如此。

关于java - 从 JTable 添加/删除行后如何添加和减去总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61138968/

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