| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2023 人关注过本帖
标题:表格实时更新问题
只看楼主 加入收藏
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
表格实时更新问题
请问下,表格如何实时更新,我现在JTable表格里显示的内容是数据库某表里的内容,如何实现数据库内容变化,JTable表也刷新对应值呢,看百度说用调用表格updateUI()方法但好像没什么反应,求指导
搜索更多相关主题的帖子: 表格 实时 更新 内容 数据库 
2018-03-19 13:38
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:10 
你要不把代码贴出来

假如人生没有梦想,和咸鱼有什么区别!
2018-03-19 14:48
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
收藏
得分:0 
回复 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();
   
    }
   
}
2018-03-19 19:14
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
回复 3楼 张贤明
你添加完之后需要去查一遍

假如人生没有梦想,和咸鱼有什么区别!
2018-03-19 22:59
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
收藏
得分:0 
回复 4楼 疯狂的小a
谢啦,我想到了一个简单的办法了,,修改表格模型里的内容就可以实现,比如model.removeRow(rowcount);只要添加这个进去就可以了
2018-03-20 16:35
快速回复:表格实时更新问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026308 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved