开发者

How to refresh a Jtable with 2 differen classes?

i am working on a school project where i am building an applet..

i have encountered a problem i cant solve, i have 2 classes...

the first class i very simple it has to methods which return an string array for table column and Object that returns the date..

the second class has some methods in it... they all return a component which is a table, and that table is used in different classes to show for example customer transactions etc..

Everything is working BUT the table is not updating??

i have tried everything from firetablechange etcetc.... but nothing is working...

first class:

import java.sql.ResultSetMetaData;

public class tableData extends CustFunk {

    public String[] colNamesTable(long custID, String query) {
        ResultSetMetaData rsmd = null;
        String[] colNames = null;

        try {

            resultSet = statement.executeQuery(query);
            rsmd = resultSet.getMetaData();
            colNames = new String[rsmd.getColumnCount()];

            for (int j = 0; j < rsmd.getColumnCount(); j++) {

                colNames[j] = rsmd.getColumnName(j + 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return colNames;
    }

    public Object[][] dataTable(long custID, String query) {

        ResultSetMetaData rsmd = null;
        Object[][] data = null;

        try {

            resultSet = statement.executeQuery(query);

            rsmd = resultSet.getMetaData();

            int rows = 0;
            while (resultSet.next()) {
                rows++;
            }

            resultSet.beforeFirst();
            data = new Object[rows][rsmd.getColumnCount()];

            for (int i = 0; i < rows; i++) {
                resultSet.next();
                for (int j = 0; j < rsmd.getColumnCount(); j++) {
 开发者_如何学Python                   data[i][j] = resultSet.getObject(j + 1);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}

the second class

public class TableClass {

    private JTable table;
    private tableData td;

    public Component displCustAccInfo(long custID, int x, int y, int width,
        int height) {
        td = new tableData();

        String sql = "SELECT accounts.Account_Name, accounts.Saldo, "
            + "useraccounts.Account_NR"
            + " from Accounts "
            + "INNER JOIN useraccounts on accounts.Account_NR=useraccounts.Account_NR"
            + " AND useraccounts.Kunde_id=" + custID + ";";

        table = new JTable(td.dataTable(custID, sql), td.colNamesTable(custID,
            sql)) {

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

        //table.tableChanged(new TableModelEvent(table.getModel())); 

        table.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
        table.setBackground(UIManager.getColor("CheckBoxMenuItem.selectionBackground"));
        table.setForeground(Color.WHITE);
        table.setBounds(x, y, width, height);

        return table;
    }
}

private class BtUpdateActionListener implements ActionListener {

    public void actionPerformed(ActionEvent arg0) {
        and from my customer GUI i call the method:
        TableClass 
        tc = new TableClass();
        tc.displCustAccInfo(custID, 154, 550, 357, 91
    }
}


DefaultTableModel m=new DefaultTableModel (td.dataTable(custID, sql), td.colNamesTable(custID, sql));

table.setModel(m);

You can reset the table's model or you have to create a smart way to chech what's changed in table and insert/update/remove appropriate rows accordingly.

Also I would add swing tag for the question.


Just use new timer(set refresh time,actionlisterner) then tell d actionlisterer what to do at the time specified. But the draw back is that if the refresh is too soon it will return an exception saying too many connections. But it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜