Frage

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 ?

War es hilfreich?

Lösung

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);

Andere Tipps

Not an excact answer, but a very important point : You should not invoke JTable#setModel() from doInBackground(). Your table runs on the EDT, while your SwingWorker runs on a separate thread. This means that you update a component on the EDT (the table) from another thread. Don't do that. Instead, return your table model from doInBackground() and set it in done(). To get the table model in SwingWorker's done(), use get(). This takes care of synchronization and variable visibility.

By definition, your application is broken as it is right now.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top