Select jComboBox 项目仅一次用于向 jTable 添加行

Select jComboBox item only once for adding row to jTable

我正在将 jCombobox 所选项目添加到 jTable,但我只想添加项目一次并更改特定单元格的值。
例如: 我已经添加了待售商品,所以当我再次选择相同的商品时,它必须相应地增加数量和价格计算而不是添加新行。
我试过使用以下代码:

private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                          
    DefaultTableModel df = (DefaultTableModel) jTable.getModel();
    String actionCommand = "comboBoxEdited";
    if (evt.getActionCommand().equals(actionCommand)) {
        Object items = jComboBox.getSelectedItem();
        String name = (String) items;
        String[] part = name.split("\t");
        String item = (part[0]);
        int qty = Integer.valueOf((part[1]));
        double price = Double.parseDouble((part[2]));
        int ids = Integer.valueOf(part[3]);

        int rows = df.getRowCount();
        df.addRow(new Object[]{part[0], part[1], part[2], part[3]});
        for (int i = 0; i < rows; i++) {
            String id = (String) jTable.getValueAt(i, 2);
            if (ids == Integer.valueOf((String) jTable.getValueAt(i, 3))) {
                int qt = qty+1;
                jTable.setValueAt(item, i, 0);
                jTable.setValueAt(qt, i, 1);
                jTable.setValueAt(price * qt, i, 2);
                jTable.setValueAt(ids, i, 3);
            } else {
                df.addRow(new Object[]{part[0], part[1], part[2], part[3]});
            }
        }
    }
}

所以使用该代码我只能添加第一行,而当再添加一个时我无法获得我想要的行为。

Here is my complete code可以直接签入IDE。
非常感谢。

看看下面的程序是否与您的想法相似。请注意我是如何使用 found 标志来构建逻辑的。

(我用“_”代替了制表符,因为它在组合框中更显眼。)

在这里,我假设组合框中的"price"不是单价。它是该组合框项目中的全部数量的价格。

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.event.*;

public class AddToTable {

  public static void main(String[] args) {

    JTable jTable = new JTable(new DefaultTableModel(
        new Object[][] {},
        new String[] {"Item", "Qty", "Price", "ID"}));

    JComboBox<String> jComboBox = new JComboBox<>();
    jComboBox.addItem("Item1 _ 2 _ 2.5 _ 101");
    jComboBox.addItem("Item2 _ 5 _ 6 _ 201");
    jComboBox.addItem("Item3 _ 3 _ 1.5 _ 301");

    jComboBox.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        Object items = jComboBox.getSelectedItem();
        String name = (String) items;
        String[] part = name.split(" _ ");
        String item = (part[0]);
        Integer qty = Integer.valueOf((part[1]));
        Double price = Double.parseDouble((part[2]));
        Integer ids = Integer.valueOf(part[3]);

        DefaultTableModel df = (DefaultTableModel) jTable.getModel();
        int rows = df.getRowCount();
        boolean found = false;
        for (int i = 0; i < rows; i++) {
          if (ids.equals(jTable.getValueAt(i, 3))) {
            Integer newQty = qty + (Integer) jTable.getValueAt(i, 1);
            Double newPrice = price + (Double) jTable.getValueAt(i, 2);
            jTable.setValueAt(newQty, i, 1);
            jTable.setValueAt(newPrice , i, 2);
            found = true;
            break;
          }
        }
        if (!found) {
          df.addRow(new Object[]{item, qty, price, ids});
        }
      }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jComboBox, BorderLayout.NORTH);
    f.getContentPane().add(new JScrollPane(jTable), BorderLayout.CENTER);
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}