Increment equal amount of service

Asked

Viewed 53 times

0

I have a table with the service code, service, quantity, value and total. When I add an ITEM 1 in the table for example, the data of this service will be added in the table. But in case I add this same ITEM 1 again in the table, I wanted to increment the amount that the user would type and not create a new row repeating, ITEM 1 and ITEM 1. follows below my code to add in the table.

I know I have to loop through the rows of the table and within the loop check if the line of the current iteration has the Item being added.
If you own it, you increase the amount of that Item and give a return; to get out of the way. If the item does not already exist in the table, the loop will go through all the rows and the execution will exit the loop, after exiting the loop you add the new Item.

But how to do this for Jtable?

private void insereItem()
{
    if (txtQtde.getValue().equals(0))
    {
        JOptionPane.showMessageDialog(this, "Informe uma quantidade para adicionar o serviço na tabela", "Atenção", JOptionPane.ERROR_MESSAGE);
        txtQtde.setBorder(BorderFactory.createLineBorder(Color.RED));
    } else
    {
        try
        {
            txtQtde.setBorder(BorderFactory.createLineBorder(Color.GRAY));
            DefaultTableModel modelo = (DefaultTableModel) tbOs.getModel();
            String valUni = txtValor.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", ".");
            double vtt = Double.parseDouble(valUni) * Integer.parseInt(txtQtde.getValue().toString());

            String total = String.valueOf(vtt);
            if (total.substring(total.lastIndexOf(".") + 1).length() < 2)
            {
                total = total + "0";
            }
            String unitario = txtValor.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", ".");
            if (unitario.substring(unitario.lastIndexOf(".") + 1).length() < 2)
            {
                unitario = unitario + "0";
            }
            Object[] linha =
            {
                txtCodigoServico.getText(), cbbServico.getSelectedItem(), txtQtde.getValue().toString(), unitario, total
            };

            modelo.addRow(linha);

            double valorSubTotal = vtt + Double.parseDouble(txtSubTotal.getText().replace("R$", "").replace(" ", "").replace(".", "").replace(",", "."));
            setValorSubTotal(valorSubTotal);
            tbOs.setModel(modelo);
            atualizaSoma();
        } catch (Exception e)
        {
        }
    }
}
  • Please add a [mcve] so that it is possible to execute the code and simulate the problem.

  • I can tell you that you probably need to write your own tablemodel and control in him this behavior.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.