Refresh Jtable

老子叫甜甜 提交于 2019-12-01 19:55:42
tddmonkey

Your JTable should update automatically when a change to the TableModel happens. I'm taking a leap here but I'm guessing that you're not using your own TableModel and just called the JTable constructor with your Vector. In this case you can get a hook on the TableModel and cast it to a DefaultTableModel and then call one its notification methods to let the JTable know of a change, something like:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.fireTableChanged(new TableModelEvent(........));

What I would really recommend is using your own TableModel unless this is something very trivial, but the fact you're updating the data indicates it's not.

Check out the sun tutorial on working with tables, inparticular the section on listening for data changes.

It might seem like more work up front, but it will save you alot of headaches in the long run and is The Right Way to do it

I call the initTable method followed by loadTable(). I'm sure there's plenty of other ways but this works like acharm.

private void initBerkheimerTable() {
        tblBerkheimer = new JTable();
        tblBerkheimer.getSelectionModel().addListSelectionListener(new SelectionListener());
        tblBerkheimer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblBerkheimer.setModel(new DefaultTableModel(
            new Object[][] {
            },
            new String[] {
                "Id", "Name", "Berkheimer PSD", "Rate", "Current PSD", "Current Rate"
            }
        ) {
            Class[] columnTypes = new Class[] {
                String.class, String.class, String.class, String.class, String.class, String.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
            boolean[] columnEditables=new boolean[]{false,false,false,false,false,false,false,false,false,false};
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        scrollPane.setViewportView(tblBerkheimer);
        add(scrollPane);
    }

private void loadTable(){
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            PayrollPsdAuditing.payroll=Database.connectToSQLServerDataBase(PayrollPsdAuditing.payrollIni);
            ps=PayrollPsdAuditing.payroll.prepareStatement(
                "SELECT a.EMPLOYID, " +
                "   a.NAME, " +
                "   a.PSD_CODE, " +
                "   a.RATE, " +
                "   b.STRINGS_I_2 as CURRENT_PSD, " +
                "   c.lcltaxrt as CURRENT_RATE " +
                "FROM PYRL_PSD_VALIDATION a, " +
                "   EX010130 b, " +
                "   UPR41400 c " +
                "WHERE a.employid=b.empid_i " +
                "   AND c.localtax=b.strings_i_2");
            rs=ps.executeQuery();
            while(rs.next()) {
                Swing.fillJTable(tblBerkheimer,
                        new String[]{rs.getString("EMPLOYID").trim()
                            ,rs.getString("NAME").trim()
                            ,rs.getString("PSD_CODE").trim()
                            ,String.valueOf(rs.getDouble("RATE"))
                            ,rs.getString("CURRENT_PSD").trim()
                            ,String.valueOf(rs.getDouble("CURRENT_RATE")/100000)});
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            Database.close(PayrollPsdAuditing.payroll);
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!