Tenho uma pequena aplicacao,
nela tem uma JTABLE que é instaciada passando uma classe que Herda AbstractTableModel,
tem outro form de cadastro na tabela que Jtable mostra!!!
preciso atualizar a Jtable depois que um insert na tabela !!! !!!
Alguma sugestao??
//// CLASSE DO ABSTRACTTABLE MODEL
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import JDBC.myConnection;
public class TableModelUsers extends AbstractTableModel implements TableModel {
private myConnection mCon = new myConnection();
private Connection con;
private Statement st;
private ResultSet rs;
private ResultSetMetaData rsmt;
private String query;
private boolean connectedToDatabase;
private int numberOfRows = 0;
public TableModelUsers(){
try {
setQuery("Select user_id 'Codigo',user_nome 'Nome', user_senha 'Senha' from user");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("Erro setQuery");
e.printStackTrace();
}
}
public int getColumnCount() {
if (!connectedToDatabase)
throw new IllegalStateException("Não conectado com o Banco de Dados!");
try{
return rsmt.getColumnCount();
}catch (SQLException e){
System.out.println("Erro getColumncount");
e.printStackTrace();
}
return 0;
}
public int getRowCount() {
if(!connectedToDatabase)
throw new IllegalStateException ("Não conectado com o Banco de Dados!");
try {
rs.last();
//System.out.printf("Linhas %d",rs.getRow());
numberOfRows = rs.getRow();
return numberOfRows;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("Erro getRowCount");
e.printStackTrace();
return 0;
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(!connectedToDatabase)
throw new IllegalStateException ("Não conectado com o Banco de Dados!");
try {
Object value;
rs.absolute(rowIndex+1);
value = rs.getObject(columnIndex+1);
if (value.equals(""))
return "tt";
else return value;
} catch (SQLException e) {
System.out.println("Erro getValueAt");
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public void setQuery(String query) throws SQLException{
con = mCon.getConnection();
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(query);
rsmt = rs.getMetaData();
connectedToDatabase = true;
fireTableStructureChanged();
fireTableDataChanged();
}
public String getColumnName(int column){
if(!connectedToDatabase)
throw new IllegalStateException("Não está conectado com o Banco de dados!");
try{
return rsmt.getColumnName(column+1);
}
catch(SQLException e){
e.printStackTrace();
}
return "";
}
//PEga a classe
public Class getColumnClass(int column){
if(!connectedToDatabase)
throw new IllegalStateException("Não conectado com o Banco!");
try{
String className = rsmt.getColumnClassName(column+1);
System.out.printf("\nClasse %s", className);
return Class.forName(className);
}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Object.class;
}
public void disconnectFromDatabase(){
if (!connectedToDatabase)
return;
try{
st.close();
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
connectedToDatabase = false;
}
}
}
//classe que mostra Jtable
public class form_users extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JButton jButton1 = null;
/**
* This is the default constructor
* @throws ClassNotFoundException
* @throws SQLException
*/
public form_users() throws SQLException, ClassNotFoundException {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
* @throws ClassNotFoundException
* @throws SQLException
*/
private void initialize() throws SQLException, ClassNotFoundException {
this.setSize(606, 500);
this.setLocationRelativeTo(null);
this.setContentPane(getJContentPane());
this.setTitle("Usuários");
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
sair(); // TODO Auto-generated Event stub windowClosing()
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
* @throws ClassNotFoundException
* @throws SQLException
*/
private JPanel getJContentPane() throws SQLException, ClassNotFoundException {
if (jContentPane == null) {
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(3);
borderLayout.setVgap(4);
jContentPane = new JPanel();
jContentPane.setLayout(borderLayout);
jContentPane.add(getJScrollPane(), BorderLayout.NORTH);
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
}
return jContentPane;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
* @throws ClassNotFoundException
* @throws SQLException
*/
private JScrollPane getJScrollPane() throws SQLException, ClassNotFoundException {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
* @param <TableModelUsers>
*
* @return javax.swing.JTable
*/
private JTable getJTable() throws SQLException, ClassNotFoundException {
if (jTable == null) {
TableModelUsers tb = new TableModelUsers();
jTable = new JTable(tb);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setFont(new Font("Dialog", Font.BOLD, 12));
}
return jTable;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(getJButton());
jPanel.add(getJButton1());
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Cadastrar");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
form_cadastrar_user cadastro = new form_cadastrar_user();
cadastro.setVisible(true);
cadastro.enable(true);
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("Remover");
}
return jButton1;
}
private void sair() {
int sair = JOptionPane.showConfirmDialog(null,"Deseja fechar o programa?","Fechando programa",JOptionPane.OK_CANCEL_OPTION);
if (sair == 0 )
System.exit(1);
}
}