【问题标题】:How to refresh JTable after inserting data to database?将数据插入数据库后如何刷新JTable?
【发布时间】:2015-02-02 19:54:37
【问题描述】:

我正在从 access 数据库中填充 JTable。第一次运行代码时,表格加载完美。然后从 JDialog 向数据库中添加新记录。我试图做的是在 JDialog 关闭时调用 loadData() 方法,但表没有更新。

这是我的loadData() 方法:

private void loadData() {
    System.out.println("sssss");
    final String [] columnNames={"Seq", "First Name", "Last Name","Num1","Num2","Num3"};
    connectDb();

    data = new Object[rows][columns];
    int row = 0;
    try {
        while(rs.next()){
            for(int col = 0 ; col<columns; col++ ){
                if(col==0)
                    data[row][col]=rs.getString("contact_seq");
                if(col==1)
                    data[row][col]=rs.getString("contact_fname");
                if(col==2)
                    data[row][col]=rs.getString("contact_lname");
                if(col==3)
                    data[row][col]=rs.getString("contact_num1");
                if(col==4)
                    data[row][col]=rs.getString("contact_num2");
                if(col==5)
                    data[row][col]=rs.getString("contact_num3");


            }
            row++;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    model = new DefaultTableModel(data, columnNames){

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int row, int column)
        {
            return false;
        }



     };

     table = new JTable(model);
}`

这就是我在关闭 JDialog 时调用 loadData 方法的方式。

JMenuItem mntmNew = new JMenuItem("New Contact");
    mntmNew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addData gui = new addData(viewData.this,rs);
            gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            gui.setVisible(true);
            gui.addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent e){
                    loadData();
                }
            });
        }
    });
    mnFile.add(mntmNew); 

添加记录时我的数据库已更新,但 Jtable 未刷新。

【问题讨论】:

    标签: java swing ms-access jtable


    【解决方案1】:

    这里:

    private void loadData() {
        ...
        table = new JTable(model); // don't re-create the table here
    }
    

    不要重新创建表,而是更新其模型,方法是设置新的表模型或清除并重新填充当前模型:

    private void loadData() {
        ...
        table.setModel(model); 
        // Of course, table should have been initialized
        // and placed first, and only once!
    }
    

    参见示例here(包括在后台线程中进行数据库调用的 SwingWorker)、herehere。请看一下这些答案,有解释可以清楚地说明这一点。

    【讨论】:

    • 感谢 dic19。这对我有用。我在构造方法中添加了table = new JTable(model),然后在构造方法中添加了loadData() 方法。我的桌子需要一些时间才能加载。所以现在我将做与您的示例相同的更改。
    【解决方案2】:

    这对我有用:

     if (model.getRowCount() > 0) {
         for (int i = model.getRowCount() - 1; i > -1; i--) {
             model.removeRow(i);
         }
     }
    
     setTablevalue();
    

    我从JTable 中删除了所有行,并再次调用setTableValue 方法重新填充表格。

    【讨论】:

      【解决方案3】:

      这是在黑暗中拍摄的,但也许这会奏效?:

      public void windowClosed(WindowEvent e) {
          loadData();
          // Add this line:
          table.repaint();
      }
      

      如果我了解发生了什么,底层数据库正在更新,但 JTable 组件没有显示更新。我的猜测是,您只需调用 repaint() 方法,JTable 也会得到更新。

      【讨论】:

      • 感谢尼希隆。这对我不起作用。但我将进一步了解repaint() 方法。
      • 不用担心@broken_code,我的猜测是错误的。大错特错。但这就是我们在这里的原因,对吧?学习!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2013-02-11
      • 2012-07-26
      • 1970-01-01
      相关资源
      最近更新 更多