如何立即查看JTable上已更改的数据?

时间:2013-11-08 14:54:01

标签: java sql swing jtable actionlistener

我已经使用了fireDataChanged方法,但我认为由于这是连接到我的数据库的事实,它不会做任何事情。

    DelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int selRow = table.getSelectedRow();            
            Object element = table.getValueAt(selRow, 0);
            th = table.getTableHeader();
            tcm = th.getColumnModel();
            Statement statement = null;
            try {
                statement = ResultSetTableModelFactory.connection
                        .createStatement();


                    if (JisSelected == true) {

                        String delete = "DELETE FROM J WHERE JNO = '" + element + "';";
                        statement.executeUpdate(delete);
                    }
                     if (SPJisSelected == true) {

                        String delete = "DELETE FROM SPJ WHERE SNO = '" + element + "';";
                        statement.executeUpdate(delete);

                    }
                System.out.println(element);
            } catch (Exception ex) {
                //ex.printStackTrace();
            }
            model.fireTableDataChanged();
        }
    });

}

1 个答案:

答案 0 :(得分:2)

您的ActionListener应该在实施SwingWorkerdoInBbackground()方法时执行其工作。在此期间,您应该以适合您的应用程序的任何方式表示操作正在等待处理,可能在渲染器或状态指示器中。如果数据库操作成功,请更新TableModel实施中的done()。在任何情况下都不应该从模型中调用fireTableDataChanged()