I can't update my JTable
from SwingWorker
Thread. My code;
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);
}
}
I execute this swingworker thread in init components of GUI for fill the jtable when program is launched.
fillAccList = new FillTable();
fillAccList.execute();
When program launch, i see [NAME] in screen because i add this line System.out.println(columnNames); for control in swingworker as you see. But my jtable not filled. Any idea ?
The constructor for DefaultTableModel expects the data first, and then the column names.
Change:
DefaultTableModel model = new DefaultTableModel(columnNames,data);
To:
DefaultTableModel model = new DefaultTableModel(data, columnNames);