Multiply 2 fields in Jtable

Asked

Viewed 319 times

1

I’m trying to get a 2-field multiplication on Jtable but I can’t do it

Foto da Tela

Specifically the Quantity Field and Unit Value to multiply and give the Total Value

Tela do Jtable

package Telas;

import Classes.Cliente;
import Classes.Material;
import Classes.Orcamento;
import Classes.Pedido;
import Hibernate.HibernateUtil;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class Tela_IncluirOrcamento extends javax.swing.JInternalFrame {


    public Tela_IncluirOrcamento() {
        initComponents();

          Btn_Pagamento.setEnabled(false);
          Btn_Total.setEnabled(false);

      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

      Session session = sessionFactory.openSession();

     List<Pedido> pedidos = session.createQuery("SELECT p FROM Pedido p JOIN FETCH p.clienteID_Cliente").list();
      for (Pedido pedido : pedidos) {
    Cliente cliente = pedido.getClienteID_Cliente();
     DefaultTableModel model = (DefaultTableModel) TBL_Orcamento.getModel();
       model.addRow(new Object[]{


        cliente.getNome(),
        pedido.getTipo_Servico()

         });

        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        Btn_Pagamento = new javax.swing.JButton();
        Btn_Valor = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TBL_Orcamento = new javax.swing.JTable();
        Btn_Total = new javax.swing.JButton();

        setClosable(true);

        Btn_Pagamento.setText("LiberarPagamento");
        Btn_Pagamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Btn_PagamentoActionPerformed(evt);
            }
        });

        Btn_Valor.setText("CalcularValorTotal");
        Btn_Valor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Btn_ValorActionPerformed(evt);
            }
        });

        TBL_Orcamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do Cliente", "Tipo de Serviço", "Descricao do produto", "Quantidade", "Valor unitario", "Valor total", "Valor Total do Orcamento"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, true, true, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TBL_Orcamento);

        Btn_Total.setText("CalcularTotalOrcamento");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGap(627, 627, 627)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(Btn_Pagamento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Btn_Valor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(Btn_Total)
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Btn_Pagamento)
                    .addComponent(Btn_Total))
                .addGap(18, 18, 18)
                .addComponent(Btn_Valor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>                        

    private void Btn_PagamentoActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:




    }                                             

    private void Btn_ValorActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:

 //Codigo Aqui

    }                                         


    // Variables declaration - do not modify                     
    private javax.swing.JButton Btn_Pagamento;
    private javax.swing.JButton Btn_Total;
    private javax.swing.JButton Btn_Valor;
    private javax.swing.JTable TBL_Orcamento;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}
  • Should the field already load with this account or only after some action? It would be interesting to add a [mcve] from your table, I couldn’t execute this code.

  • the 2 fields first already load a query to make a Join of dps tables in the button "Release payment",the code goes in Btn_valor la below

  • I just want code on Jtable to do the multiplication so you can disregard it up there

  • Got it, can you add one [mcve] of your table with some of the data it will be?

  • Code Ready ? updated image

1 answer

0


Try the following:

private void Btn_ValorActionPerformed(java.awt.event.ActionEvent evt) {                                          
    double valorUnitario_col;
    int quantidade_col;

        for (int i = 0; i < TBL_Orcamento.getRowCount(); i++) {
            quantidade_col = Integer.parseInt((String)TBL_Orcamento.getValueAt(i, 3));
            valorUnitario_col =  Double.parseDouble((String)TBL_Orcamento.getValueAt(i, 4));
            TBL_Orcamento.setValueAt((quantidade_col * valorUnitario_col), i, 5);
        }

}

In this code, when pressing the button, a loop will be made that will multiply the columns of quantidade and valor unitario and place the result in the column valor total in all rows the table has.

  • Thank you worked well

Browser other questions tagged

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