Do Multiply 2 Fields in JTable

1

I'm trying to get a multiplication of 2 fields in JTable but I can not do

SpecificallytheFieldQuantityandUnitValuetomultiplyandgivetheTotalValue

JTableScreen

packageTelas;importClasses.Cliente;importClasses.Material;importClasses.Orcamento;importClasses.Pedido;importHibernate.HibernateUtil;importjava.util.Iterator;importjava.util.List;importjavax.swing.table.DefaultTableModel;importorg.hibernate.Session;importorg.hibernate.SessionFactory;publicclassTela_IncluirOrcamentoextendsjavax.swing.JInternalFrame{publicTela_IncluirOrcamento(){initComponents();Btn_Pagamento.setEnabled(false);Btn_Total.setEnabled(false);SessionFactorysessionFactory=HibernateUtil.getSessionFactory();Sessionsession=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                   
}
    
asked by anonymous 23.02.2017 / 23:49

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 you press the button, a loop will be made that will multiply the columns of quantidade and valor unitario and put the result in column valor total in all rows that the table has.     

24.02.2017 / 00:31