我有两个JFrame
例子。Main frame 具有将数据从 DB 填充到 table 的代码,另一个是 jFrame2,而用户从 Main Frame 添加或更新记录表将自动刷新
这是我的主框架代码:
public void fillTable(){
String sql = "SELECT ID, UniqueCode, Name, Age, Gender, Cell FROM info";
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(92);
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
这是我的第 2 帧代码
private void save(){
try{
if(name.getText().isEmpty() || age.getText().isEmpty() || cell.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}
else if(" ".equals(cmbGender.getSelectedItem())){
JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}
else{
connect = mysqlconnect.DBConnect();
stmt = connect.createStatement();
rs = stmt.executeQuery("SELECT Name FROM info WHERE Name='" + name.getText() + "'");
if(rs.first()){
JOptionPane.showMessageDialog(null, "Name already Exist, Duplicate info is not allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}else{
sql = "INSERT INTO info(UniqueCode, Name, Age, Gender, Cell) values ('" + uniqueID.getText()+ "', '" + name.getText() + "', '" + age.getText() + "', " +
" '" + cmbGender.getSelectedItem() + "', '" + cell.getText() + "') ";
Statement st = connect.createStatement();
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Successfully Saved", "System Message", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
frame.jTable1.repaint(); <====== Error comes here because i want to refresh my table
}
}
}
catch(Exception ex){
System.out.println(ex);
}