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

1

I have a table (jTable) that displays all the data registered in a database. My table is populated every time the application is started through a WindowEvent, but when I make a new record in the database through another frame I can not get my jTable updated automatically to also display the new registered record. As it stands, the only way to view the new data that is registered in the DB is to terminate the application and start 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();
            }
        }
    
asked by anonymous 22.11.2014 / 18:51

1 answer

1

You can try to put your method as static and call it when the insert in the BD is finished.

public static void windowOpened(java.awt.event.WindowEvent e) {

The flame looks like this:

SuaClasse.windowOpened(null);

Another option is to use Threads to keep updating every time, from a search in the subject, I even recommend this article:

link

    
25.11.2014 / 17:51