如何在从JTable中添加/移除行后,对总和的值进行加法和减法操作

huangapple 未分类评论48阅读模式
英文:

How to add and subtract value of sum after adding/removing a row from 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() {  // 列 02 是商品价格所在列
    for (int i = 0; i < cartTable.getRowCount(); i++) {
        total += Double.parseDouble(cartTable.getValueAt(i, 2).toString()); 
    }
    cartAmount.setText("总计: P " + Double.toString(total));
}

// 从购物车中移除商品按钮代码:
try {
    for (int i = 0; i < cartTable.getRowCount(); i++) {
        total = total - Double.parseDouble(cartTable.getValueAt(i, 2).toString());
    }
    cartAmount.setText("总计: P " + Double.toString(total));
    if (total <= 0.0) {
        total = 0.0;
    }
    {
        int getSelectedRowForDeletion = cartTable.getSelectedRow();
        model2.removeRow(getSelectedRowForDeletion);
        JOptionPane.showMessageDialog(null, "从购物车中移除商品");
    }
} catch (NumberFormatException ex) {
    ex.printStackTrace();
} catch (ArrayIndexOutOfBoundsException ex) {
}

如何使“移除”按钮在没有选择行时无法工作?提示用户在删除前选择一行?还要避免负数的总计计算。谢谢。

英文:

I have the code for adding and removing a single row for my JTable but it causes problems like not providing accurate calculation of overall sum and subtraction after removing it from the JTable. The JTable serves as a "cart" for adding and removing items in the cart.

The add to cart button for adding a row to the cart:

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 &lt; 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();
        }

The code where it adds the overall items:

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

}

The remove an item from the cart button code:

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

Here are some pics when adding and remove an item:
如何在从JTable中添加/移除行后,对总和的值进行加法和减法操作

如何在从JTable中添加/移除行后,对总和的值进行加法和减法操作

如何在从JTable中添加/移除行后,对总和的值进行加法和减法操作

How can I also make the remove button to not work when no row is selected? Like ask the user to select a row before deletion? Also remove the possibility of having negative sum calculations. Thank you

答案1

得分: 0

我解决了它:

添加到购物车按钮:

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 中的下一项。移除按钮同理。

英文:

I solved it:

The add to cart button:

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

            for (int i = 0; i &lt; 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(&quot;Total: P &quot; + Double.toString(total));
            }catch (ArrayIndexOutOfBoundsException a) {
        }
    }

The remove button:

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

I removed the for loop when adding the values as it will just add the next item in the productTable. Same goes for the remove button.

huangapple
  • 本文由 发表于 2020年4月10日 19:04:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61138968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定