咸鱼梦源 2020-09-02 13:16 采纳率: 100%
浏览 299
已采纳

java JScrollPane与jtable1怎么刷新表格?

package PFMS;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class incomeinquireset extends JFrame{

    incomeinquireset(){
        this.setLayout(null);
        setTitle("个人财务管理——修改收入");
        Object[][] a = new Object[20][5] ;
        Object name[] = {"编号","日期","金额","原因","备注"};
        setLocation(650, 400);
        JLabel jno=new JLabel("请输入编号:");
        JLabel jdata=new JLabel("请输入日期:");
        JLabel jcount=new JLabel("请输入金额:");
        //JLabel jtest=new JLabel("备注:");
        JButton jbdel=new JButton("删除");
        JButton jbdata=new JButton("查询日期");
        JButton jbcount=new JButton("查询金额");
        JTextField jtno=new JTextField();
        JTextField jtdata=new JTextField();
        JTextField jtcount=new JTextField();
        add(jdata);
        add(jno);
        add(jcount);
        add(jbcount);
        add(jtdata);
        add(jtcount);
        add(jbdel);
        add(jbdata);
        add(jtno);
        jno.setBounds(new Rectangle(80, 10, 80, 30));
        jdata.setBounds(new Rectangle(80, 50,80, 30));
        jcount.setBounds(new Rectangle(80, 90, 80, 30));
        jtno.setBounds(new Rectangle(170, 10, 80, 30));
        jtdata.setBounds(new Rectangle(170, 50,80, 30));
        jtcount.setBounds(new Rectangle(170, 90, 80, 30));
        jbdel.setBounds(new Rectangle(300, 10, 100, 30));
        jbdata.setBounds(new Rectangle(300,50,100,30));
        jbcount.setBounds(new Rectangle(300,90,100,30));
        JTable jtable1 = new JTable();
         JScrollPane jscrollpane1 = new JScrollPane();

        this.setSize(600, 400);
        this.setVisible(true);
        try
         {
            Class.forName(global.driverName);
        Connection dbConn  = DriverManager.getConnection(global.dbURL, global.userName, global.userPwd);
            Statement ps = dbConn.createStatement();  
             ResultSet rs=ps.executeQuery("select * from 收入");
             int k=0;

             while(rs.next()){
                a[k][0]=rs.getString(1);
                a[k][1]=rs.getString(2);
                a[k][2]=rs.getString(3);
                a[k][3]=rs.getString(4);
                a[k][4]=rs.getString(5);
                 k++;
             }

             jtable1 = new JTable(a, name);
             add((jtable1));
             jscrollpane1=new JScrollPane(jtable1);
             add(jscrollpane1);
             jscrollpane1.setBounds(new Rectangle(50,140,500,169));
             }catch(Exception e1){
                JOptionPane.showInputDialog(this,   "查询失败");
             }




        jbdel.addActionListener(new ActionListener(){
             @Override
              public void actionPerformed(ActionEvent e) {
                 String no=jtno.getText().trim(); 
                    try
                 {
                    Class.forName(global.driverName);
                    Connection dbConn  = DriverManager.getConnection(global.dbURL, global.userName, global.userPwd);
                    Statement ps = dbConn.createStatement();  
                    int k;
                    k=ps.executeUpdate("DELETE FROM 收入 WHERE 编号='"+no+"'");
                    if(k==1) {
                        JOptionPane.showMessageDialog(null, "删除成功");
                    }else {
                        JOptionPane.showMessageDialog(null, "删除失败");
                    }

                 }catch(Exception e1){
                     JOptionPane.showMessageDialog(null, "请检查输入内容是否正确!");
                 }


                    try
                 {
                    Class.forName(global.driverName);
                Connection dbConn  = DriverManager.getConnection(global.dbURL, global.userName, global.userPwd);
                    Statement ps = dbConn.createStatement();  
                     ResultSet rs=ps.executeQuery("select * from 收入");
                     int k=0;
                    Object[][] b = new Object[20][5] ;
                     while(rs.next()){
                        b[k][0]=rs.getString(1);
                        b[k][1]=rs.getString(2);
                        b[k][2]=rs.getString(3);
                        b[k][3]=rs.getString(4);
                        b[k][4]=rs.getString(5);
                         k++;
                     }
                    //JTable jtable1 = new JTable();
                    //jtable1 = new JTable(b, name);
                    //add(jtable1);
                    //JScrollPane jscrollpane1=new JScrollPane(jtable1);
                    //jscrollpane1.getViewport().add(jtable1);
                    //add(jscrollpane1);
                    //jscrollpane1.setViewportView(jtable1);
                    //jscrollpane1.validate();
                    //jscrollpane1.setBounds(new Rectangle(40,140,500,169));
                     }catch(Exception e1){
                        JOptionPane.showInputDialog(this, "查询失败");
                     }

    }});}
    public static void main(String[] args) {
        incomeinquireset f = new incomeinquireset();
    }

}
页面一加载就会画出表格,然后当删除按钮被按下的时候,表格会刷新。
这个按下按钮刷新表格的功能有没有大佬会啊,求助,谢谢大佬
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-09-02 13:53
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面
  • ¥15 各位帮帮我 我不想重做系统
  • ¥30 微信小程序蓝牙数据透传
  • ¥15 加氢站氢负荷数据集来源
  • ¥15 umi接入sentry遇到问题