Reputation: 245
My JTable doesn't refresh after new data is entered into the database. I need to terminate and execute again to see results. I've included DefaultTableModel and fireTableDataChanged. What else did I miss? Thanks!
{
columnNames = new Vector();
data = new Vector();
try{
//database stuffs
}
catch{
}
DefaultTableModel tm = new DefaultTableModel();
JTable table = new JTable (tm);
JScrollPane scrollPane = new JScrollPane(table);
table = new JTable(data,columnNames)
{
public boolean isCellEditable(int row, int column){
return false;
}
public void newDataAvailable (TableModelEvent e){
}
};
scrollPane = new JScrollPane(table);
scrollPane.setBounds(33, 47, 256, 228);
panel.add(scrollPane);
}
Can I create a 'Refresh Button' and refresh the values instead?
Upvotes: 2
Views: 1869
Reputation: 31
2.create a table in back-end…
Right click on the project > properties > library > add Jar/folder > select jar > ok
5.Then connect mysql using following code
public class DBConnect {
Connection connection = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
System.out.println("connection successfully");
} catch (Exception ex) {
ex.printStackTrace();
}
return connection;
}
}
Call the function in the page using following code
con=new DBConnect();
statement=con.getConnection().createStatement();
7.Then create a update function type the following code…
void update_table() throws SQLException{
rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
8.In the buttons action event type the following and call the update function
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Object name=jTextField1.getText();
String sql="insert into details (name) values('"+name+"')";
int resultset=statement.executeUpdate(sql);
update_table();
} catch (SQLException ex) {
Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
}
}
9.Then run the project…
YOU can also Follow this Tutorial:--
How to refresh JTable after insert delete or update the data in java Netbeans ID
Upvotes: 0
Reputation: 57381
You create new JTable
instances. Create it just once and set the new model only to the existing JTable
(added to container properly).
Upvotes: 2