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();
}
}