user10738240
user10738240

Reputation:

Select jComboBox item only once for adding row to jTable

I'm adding jCombobox selected item to jTable but I want to add item only once and change that specific cells values .
For example: I've added item for sale so when I'm selecting same item once again it must increase quantity and price accordingly calculation than adding new row.
I've tried using following code :

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

So with that code I can add only first row and when adding one more I can not get behavior I want.

Here is my complete code can check in IDE directly.
Thank you so much.

Upvotes: 0

Views: 262

Answers (1)

Prasad Karunagoda
Prasad Karunagoda

Reputation: 2148

See whether below program works similar to what is in your mind. Notice how I have used found flag to build the logic.

(I have used " _ " in place of the tab character because it is more visible in the combo box.)

Here, I have assumed the "price" in the combo box is not the unit price. It is the price of full quantity in that combo box item.

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

Upvotes: 0

Related Questions