表格实时更新问题
请问下,表格如何实时更新,我现在JTable表格里显示的内容是数据库某表里的内容,如何实现数据库内容变化,JTable表也刷新对应值呢,看百度说用调用表格updateUI()方法但好像没什么反应,求指导
回复 2楼 疯狂的小a
代码如下,想实现的功能是增加用户,或者删除用户,,下面的显示框会实时更新,不需要重进界面才会刷新出刚才增加或删除之后的内容,我尝试过把显示内容那部分定义成一个方法,但好像也不是很好,而且麻烦,import java.awt.*;
import java.awt.event.*;
import *;
import *;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Conn {
public void Myframe(){
JFrame qj=new JFrame("QQ2017");
qj.setLayout(null);
qj.setBounds(0,0,200,500);
Container con=qj.getContentPane();
qj.setVisible(true);
JButton user=new JButton("管理用户");
user.setBounds(30,60,100,21);
user.setForeground(Color.red);
con.add(user);
user.setVisible(true);
user.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFrame zjadmin=new JFrame("用户");
zjadmin.setLayout(null);
zjadmin.setBounds(0,0,300,300);
zjadmin.setVisible(true);
Container con1=zjadmin.getContentPane();
//用户框内的面板显示
Connection con;
ResultSet res;
DefaultTableModel model=new DefaultTableModel();//创建一个表格模型
model.setColumnIdentifiers(new Object[]{"账号","密码",});//创建表头
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("驱动加载成功");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
System.out.println("数据库连接成功");
String sql="select * from USERINTO";
PreparedStatement stmt=con.prepareStatement(sql);
res=stmt.executeQuery();
while(res.next()){
String id=res.getString("userid");
String password=res.getString("password");
model.addRow(new Object[]{id,password});
}
final JTable table=new JTable(model);
JScrollPane scrollPanne=new JScrollPane(table);
scrollPanne.setBounds(0,80,300,200);
con1.add(scrollPanne);
scrollPanne.setVisible(true);
JButton addadminer=new JButton("增加用户");
addadminer.setBounds(10,10,100,21);
con1.add(addadminer);
addadminer.setVisible(true);
addadminer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame zjadmin=new JFrame("增加用户");
zjadmin.setBounds(0,0,300,200);
Container con1=zjadmin.getContentPane();
zjadmin.setVisible(true);
final JLabel zh=new JLabel("账号");
zh.setBounds(0, 20, 100, 21);
con1.add(zh);
final JTextField zhangh=new JTextField();
zhangh.setBounds(30,20,100,20);
zhangh.setBackground(Color.yellow);
con1.add(zhangh);
zhangh.setVisible(true);
final JLabel zcjl=new JLabel("密码");
zcjl.setBounds(0, 40, 100, 21);
con1.add(zcjl);
final JTextField zcmima=new JTextField("",20);
zcmima.setBounds(30,40,100,20);
zcmima.setBackground(Color.yellow);
con1.add(zcmima);
zcmima.setVisible(true);
final JButton zcau=new JButton("注册");
zcau.setBounds(40,70,60,20);
con1.add(zcau);
zcau.setVisible(true);
final JLabel zcview=new JLabel();
zcview.setBounds(150, 70, 100,21);
con1.add(zcview);
zcau.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Connection con;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//System.out.println("驱动加载成功");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
//System.out.println("数据库连接成功");
String sql="insert into USERINTO (userid,password) values (?,?)";
PreparedStatement stmt=con.prepareStatement(sql);//执行sql语句
stmt.setString(1, zhangh.getText());
stmt.setString(2, zcmima.getText());
stmt.executeUpdate();
zcview.setText("注册成功");
con.close();
System.out.println("添加成功");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
});
JButton deleteadmin=new JButton("删除用户");
deleteadmin.setBounds(120,10,100,21);
con1.add(deleteadmin);
deleteadmin.setVisible(true);
deleteadmin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int rowcount=table.getSelectedRow();
System.out.print(rowcount);
System.out.print(table.getValueAt(rowcount,0 ));
Connection con;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//System.out.println("驱动加载成功");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
//System.out.println("数据库连接成功");
String sql="delete from USERINTO "+"where userid=?";
PreparedStatement stmt=con.prepareStatement(sql);//执行sql语句
stmt.setString(1, (String) table.getValueAt(rowcount,0 ));
stmt.executeUpdate();
System.out.println("删除成功");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
res.close();
con.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//addadminer.setForeground(Color.red);
}
});
}
public static void main(String args[]){
Conn main=new Conn();
main.Myframe();
}
}