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

标签 java swing jtable addition subtraction

我有为 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/

相关文章:

java - 用韩语解析日期

java - JTable.setRowHeight 阻止我添加更多行

java - Jtable 未填充一条记录

java - 分析 java 堆转储 : why are enum instances kept in heap?

java - 将参数转换为 POJO 以查询 H2 数据库

java - 在数组中搜索特定字符串

java - 如何使用 AbstractDataModel 过滤 JTable?

java - 如何显示从 JTable 连接到 MS Access 数据库到文本字段的单击行

java - 从 JTextField 获取字符串输入并将该字符串设置为 JLabel

java - 使用 JComboBox(类别) 更改/更新 JTable 内容