My Jtable does not update when I add a Row [Solved]

Asked

Viewed 27 times

0

I have a Jtable in my Mainform that receives data from a form in another window only after receiving the data Jtable does not update.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
Connection conn = null;
PreparedStatement psm = null;
String cod = txtCod.getText();
String sql = "SELECT * FROM produtos WHERE cod = "+cod;
String nome = null;
float valor = 0;
String desc = null;
try{
    conn = DatabaseConnection.MysqlConnection();
    psm = conn.prepareStatement(sql);
    psm.execute();

    ResultSet result = psm.executeQuery(sql);

    while(result.next()){
        nome = result.getString("nome");
        valor = result.getFloat("valor_m2");
        desc = result.getString("descricao");
    }
    if(txtHeight.getText().equals("") && txtWidth.getText().equals("")){
        float a2 = Float.parseFloat(JOptionPane.showInputDialog(rootPane, "Por favor insira a área da peça", "Área", HEIGHT));

        float valorm = a2 * valor;

        MainForm mf = new MainForm();
        mf.addInTable(cod, nome, a2, valor, valorm);

        this.setVisible(false);
        psm.close();
        conn.close();
    }
    else{
        float alt = Float.parseFloat(txtHeight.getText());
        float lag = Float.parseFloat(txtWidth.getText());

        float area = alt * lag;
        float valorm = area * valor;

        MainForm mf = new MainForm();
        mf.addInTable(cod, nome, area, valor, valorm);

        this.setVisible(false);
        psm.close();
        conn.close();
    }
}
catch(Exception ex){
    System.out.println(ex);;
}

Here he will create the survey and send it to Jtable on Mainform

public void addInTable(String cod, String prod, float area, float prec, float precTot){       
String nom = prod;
String con = cod;
String are = Float.toString(area);
String pre = Float.toString(prec);
String tot = Float.toString(precTot);

DefaultTableModel model = (DefaultTableModel) tblMain.getModel();
model.addRow(new Object[]{con, nom, are, pre, tot});

float total = 0;
total = total + precTot;


lblTrue.setText(Float.toString(total));

And here he adds the data to Jtable. Only how I said it doesn’t update the table and doesn’t show the data.

1 answer

0


Each time you enter these conditional links of your DAO class, it creates a new Mainform instance and sends the data so it’s not right.

Try to create a class that has all these attributes that you want to return and then in your DAO class create a method like for example:

public List<EssaClasseVolta> findAll(){
}

and there on your mainframe you create a list and get one in that way. Soon after is only popular using for to browse the list.

  • the problem is not popular the table and yes add in the table my friend pointed me the same fact mainForm create a new instance. But I’ve already arranged it, but thank you so much for your help.

Browser other questions tagged

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