0

I using detaful table model

DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

and I row count

int Row = model.getRowCount();

Here is which I add row and wish to diplay error when adding same items.

if (Row > 0) {

               for(i=0;i<Row; i++){
                   if(jTable1.getValueAt(i,0).equals(name.getText())){       
                    JOptionPane.showMessageDialog(null, "Can't add same item");
               }
               }
    }else if (Row <99) {
        model.addRow(new Object[] {name.getText(),address.getText(),Integer.parseInt(age.getText())});
    }

I guess wrong at if else statement? Because I can add the 1st row and I can't add for 2nd row.

Thanks for help

Q123Q
  • 9
  • 1
  • 9

1 Answers1

0

Not sure what you have in your other variables, but I guess you have a discrepancy between product_id and name, perhaps?

Wouldn't it be more like

if(jTable1.getValueAt(i,0).equals(name.getText())){

UPDATE:

Try rewriting it like this:

int Row = model.getRowCount();
int boolean exists = false;
for(i=0;i<Row; i++){
   if(jTable1.getValueAt(i,0).equals(name.getText())){       
      JOptionPane.showMessageDialog(null, "Can't add same item");
      exists = true;
      break;
   }       
}


if (!exists && Row < 99) {
     model.addRow(new Object[] {name.getText(),address.getText(),Integer.parseInt(age.getText())});
}
Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44