How to update a jTable that displays BD data after registering a new item?

Asked

Viewed 1,996 times

1

I have a table (jTable) that displays all the data registered in a database. My table is filled every time the application is started through a Windowevent, but when I make a new registration in the database through another frame I cannot make my jTable be automatically updated to also display the new registration. The way it is, the only way to visualize the new data registered in the BD is by terminating the application and starting it again.

//Modelo da tabela
DefaultTableModel listalivros = new DefaultTableModel();

//Tabela que exibe os dados
 private JTable getJTable() {
    if (jTable == null) {
        jTable = new JTable(listalivros);
        jTable.setFont(new Font("Dialog", Font.PLAIN, 12));
        listalivros.addColumn("ISBN");
        listalivros.addColumn("Título");
        listalivros.addColumn("Autor");
        listalivros.addColumn("Editora");
        listalivros.addColumn("Cosignação");
        listalivros.addColumn("Preço");
        listalivros.addColumn("Quantidade");
    }
    return jTable;
}

// Evento que preenche a tabela 
          public void windowOpened(java.awt.event.WindowEvent e) {
              System.out.println("windowOpened()"); // TODO Auto-generated
            listalivros.setNumRows(0);
              try {
                index.Executar("select * from livros");
                index.Resultado.next();
                rs = index.Resultado;
                do{
                    ISBN = (rs.getString("ISBN"));
                    TituloLivro = (rs.getString("titulo_livro"));
                    AutorLivro = (rs.getString("autor_livro"));
                    EditoraLivro = (rs.getString("editora_livro"));
                    Consignacao = (rs.getString("consignacao"));
                    Preco = (rs.getString("preco"));
                    Quantidade = (rs.getString("quantidade"));
                     listalivros.addRow(new Object[] { ISBN, TituloLivro,
                            AutorLivro, EditoraLivro, Consignacao, Preco,
                            Quantidade });
                }while (rs.next());
            } catch (SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
        }

1 answer

1

  • I was able to solve by creating a Static method to update my jTable to each registration.

Browser other questions tagged

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