How to increment a specific cell in jTable Model

Viewed 37

On POS screen i am adding data to a jTable Data Model using below code. If there are more than one items with same barcode I don't want to add another row but want to only increment quantity and update net and total price accordingly on existing row.

    private void jBCodeKeyPressed(java.awt.event.KeyEvent evt) {                                                                  
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {

            if (jBCode.getText().isBlank() || c_search_tbl.getText().isBlank()){
                JOptionPane.showMessageDialog(null, "No Matching Product");
                u_disc_pct.setText("0");
                s_unit.setSelectedIndex(0);
            }    
            else {
                DefaultTableModel dt = (DefaultTableModel) jTable1.getModel();
    
                String entered_qty = JOptionPane.showInputDialog("Enter Quantity");
                int q = Integer.parseInt(entered_qty);
                double up = Double.parseDouble(p_u_price.getText());
                Double tp = q* up;
                Double pct_disc= Double.parseDouble(u_disc_pct.getText());
                Double saved_am = (tp*pct_disc)/100;
                Double nt_price = tp - saved_am;
                
                Vector v = new Vector();
               
                v.add(inid.getText()); // invoice id
                v.add(c_search_tbl.getText());
                v.add(jBCode.getText()); // barcode
                v.add(q);
                v.add(s_unit.getSelectedItem().toString());
                v.add(p_u_price.getText()); // unit price   
                v.add(tp);
                v.add(u_disc_pct.getText()+"%");  
                v.add(saved_am);
                v.add(nt_price);
                dt.addRow(v);
            }
0 Answers
Related