我無法更新我的JTable
從SwingWorker
線程。我的代碼;從Swingworker線程填充jTable
public class FillTable extends SwingWorker<Void, Void> {
protected Void doInBackground() throws Exception {
ResultSet rsaccounts;
Statement stmt;
String queryaccounts = "select NAME from acc (nolock)\n" + "order by Name";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.100.100.23;" + "databaseName=Dbacc;" + "user=" + "sa" + ";" + "password=" + "sapassword!" + ";";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
rsaccounts = stmt.executeQuery(queryaccounts);
ResultSetMetaData rsmd = rsaccounts.getMetaData();
Vector<String> columnNames = new Vector<String>();
columnNames.add(rsmd.getColumnName(1));
System.out.println(columnNames);
int columnCount = rsmd.getColumnCount();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rsaccounts.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rsaccounts.getObject(columnIndex));
}
data.add(vector);
}
DefaultTableModel model = new DefaultTableModel(columnNames,data);
jTable3.setModel(model);
rsaccounts.close();
stmt.close();
return null;
}
public void done() {
SearchButton.setEnabled(true);
CreateButton.setEnabled(true);
}
}
我在GUI的init組件中執行這個swingworker線程來填充程序啓動時的jtable。
fillAccList = new FillTable();
fillAccList.execute();
當程序啓動時,我在屏幕上看到[NAME],因爲我添加了這行System.out.println(columnNames);如你所見,用於擺動裝置的控制。但我的jtable沒有填充。任何想法 ?
我會認爲這將工作。設置模型後,您可以嘗試在JTable上進行無效操作。 – Pace
我現在嘗試,但沒有變化。 –
啊,構造函數應該是'DefaultTableModel(data,columnNames)'。你有它倒過來。 – Pace