Question

I have Swingworker for create table model for update the jTable when program is launched. But i dont know how can i call this model from EDT and apply this model to my jtable ?

Here my Swingworker code;

public class FillTable extends SwingWorker<DefaultTableModel, DefaultTableModel> {
    private final DefaultTableModel model;
    public FillTable(DefaultTableModel model) {
     this.model = model;
    }

   @Override
 protected DefaultTableModel 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);
       }

        rsaccounts.close();
        stmt.close();
        model = new DefaultTableModel(data, columnNames);
        return model;
    }


  protected void done() {
    try {
        TableModel model = get();
    } catch (InterruptedException | ExecutionException ex) {
        ex.printStackTrace();
    }
  }

}

Below method return error; Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot set a null TableModel

private DefaultTableModel model1;

FillTable fff = new FillTable(model1);
        fff.execute();
        model1 = fff.model;

        jTable3.setModel(model1);
Was it helpful?

Solution

When you are executing something like

FillTable fff = new FillTable(model1);
fff.execute();
model1 = fff.model;
jTable3.setModel(model1);

then the line model1 = fff.model will most likely be executed before the SwingWorker has finished. The execute call will cause the work to be done in the background, and the next lines well be executed regardless of whether the work already finished or not.

A general approach could be to use the model in your done() method, roughly like this:

protected void done() {
    try {
        TableModel model = get();

        jTable3.setModel(model); // Use it here!

    } catch (InterruptedException | ExecutionException ex) {
        ex.printStackTrace();
    }
}

(You'll have to pass a reference of jTable3 to your SwingWorker for this - that is, pass it to the constructor and store it as a field in the SwingWorker).

There may be more elegant solutions, but based on the code that was provided so far, this at least seems to be the reason for the error and a possible solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top