Pregunta

I'm working on a pos (like openbravo or dolibarr) panel in Java. When a user presses a JButton after getting the product info, the product is added to a JTable. However, if he presses the button twice or more, the same product is added again, so I could have a JTable with 2,3 or 4 same lines. That's why I'm trying to limit that.

My idea, is to compare the cells containing product id in the JTable and the variable containing product id in the product info form.

The problem is, when I open the panel for the first time, my JTable is empty, so my if statement can't work.

I need some advice.

jButton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
    if(idprod!=0)
    {   
        montanttva1 = prixtotalttc - prixtotalht;
        int row = tableaupan.getRowCount();
        for (int i = 0; i < row; i++){
            int test =(int) (tableaupan.getModel().getValueAt(i,0)); 
            if (test==idprod)
            {
                JOptionPane.showMessageDialog(null, "Product is already in the basket !","Error",JOptionPane.ERROR_MESSAGE);     
            }
            else
            {
            modele.addCaisse(new Caisse(idprod,JTitre.getText().toString(),cqte,prixunittcc,prixht,montanttva1,prixtotalht,prixtotalttc,typetva,tva));
            }
        }
    }
    else
    {
        JOptionPane.showMessageDialog(null, "Nothing to add in the basket !", "Errorr",JOptionPane.ERROR_MESSAGE);                  
    }
}
});

Thanks, with your comments I understand my logic error. Now I must add a method like this in my model ?

public boolean containsId(int idprod) {
*condition for true*
return true;
*condition for false*
return false;
}
¿Fue útil?

Solución 2

I've found this solution, and It works :

jButton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(idprod!=0)
{
    montanttva1 = prixtotalttc - prixtotalht;
    int row = tableaupan.getRowCount();

    for (int i = 0; i < row; i++){
        if(tableaupan.getValueAt(i,0).equals(idprod))
        {
             OptionPane.showMessageDialog(null, "Product is already in the basket !","Error",JOptionPane.ERROR_MESSAGE);     
             return;                       
        }
    }
modele.addCaisse(new Caisse(idprod,JTitre.getText().toString(),cqte,prixunittcc,prixht,montanttva1,prixtotalht,prixtotalttc,typetva,tva));
}
else
{
    JOptionPane.showMessageDialog(null, "Nothing to add to basket !", "Errorr",JOptionPane.ERROR_MESSAGE);
}
}
});  

Otros consejos

Your logic is incorrect. You compare your ID with every row. And for each row, if the row'sID doesn't match with the product ID, you add a new row.

The logic should be:

if (modele.containsId(idprod)) {
    addProduct();
}
else {
    showError();
}

Now you just need to add the containsId() method to your model.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top