title: Java: Updating JTable with lots of rows in the background
body: <p>I am writing a simple Java Swing utility that will read messages from an MQ JMS server and display them in a JTable. </p>
  
  <pre><code>private void getMessages() {
          try {
              if (null != Queue) {
                  Queue.close();  //Close previous queue connection if there is one.
              }
              Queue = new MQQueue(QueueManager, tableQueues.getValueAt(tableQueues.getSelectedRow(), 1).toString(), MQConstants.MQOO_INPUT_SHARED | MQConstants.MQOO_BROWSE | MQConstants.MQOO_OUTPUT, queueManager, null, null);
              int count = 0;
              modelMessages.setRowCount(0);
              MQGetMessageOptions getOptions = new MQGetMessageOptions();
              getOptions.options = MQConstants.MQGMO_BROWSE_FIRST;
              ArrayList&lt;Object[]&gt; rows = new ArrayList&lt;Object[]&gt;();
              while(true) {
                  if (count &gt; 0) {
                      getOptions.options = MQConstants.MQGMO_BROWSE_NEXT;
                  }
                  MQMessage message = new MQMessage();
                  try {
                      Queue.get(message, getOptions);
                      byte[] b = new byte[message.getMessageLength()];
                      message.readFully(b);
                      rows.add(new Object[]{count + 1, new String(b)});
                      modelMessages.addRow(new Object[]{count + 1, new String(b)});
                      message.clearMessage();
                      count++;
                  }
                  catch (IOException e) {
                      break;
                  }
                  catch (MQException e) {
                      if (e.completionCode == 2 &amp;&amp; e.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE) {
                          break;
                      }
                  }
              }
              modelMessages.fireTableDataChanged();
          } catch (MQException e) {
              txtMessage.setText("MQJE001: Completion Code '" + e.completionCode + "', Reason '" + e.reasonCode + "'.");
              modelMessages.setRowCount(0);
              modelMessages.fireTableDataChanged();
          }
      }
  </code></pre>
  
  <p>This works well for smaller queues but for large queues, it takes a while to populate this table and in the meantime, the Swing application is frozen so I am investigating ways to populate the JTable in the background while keeping not only the application but the JTable itself usable and scrollable during this.</p>
  
  <p>I'm not very familiar with threading and I've tried a few things such wrapping some parts in SwingUtilities.invokeLater and implementing doInBackground() but nothing has seemed to work. Could someone please point me in the right direction of how to proceed with this?</p>
  
  <p>EDIT</p>
  
  <p>Based on the replies below, here is the solution:</p>
  
  <pre><code>public class GetMessagesWorker extends SwingWorker&lt;DefaultTableModel, Object[]&gt; {
  
      //http://stackoverflow.com/questions/22072480/java-updating-jtable-with-lots-of-rows-in-the-background#
  
      private final DefaultTableModel model;
  
      public GetMessagesWorker(DefaultTableModel model){
          this.model = model;
      }
  
      @Override
      protected DefaultTableModel doInBackground() throws Exception {
          try {
              if (null != Queue) {
                  Queue.close();  //Close previous queue connection if there is one.
              }
              Queue = new MQQueue(QueueManager, tableQueues.getValueAt(tableQueues.getSelectedRow(), 1).toString(), MQConstants.MQOO_INPUT_SHARED | MQConstants.MQOO_BROWSE | MQConstants.MQOO_OUTPUT, queueManager, null, null);
              int count = 0;
              modelMessages.setRowCount(0);
              MQGetMessageOptions getOptions = new MQGetMessageOptions();
              getOptions.options = MQConstants.MQGMO_BROWSE_FIRST;
              while(true) {
                  if (count &gt; 0) {
                      getOptions.options = MQConstants.MQGMO_BROWSE_NEXT;
                  }
                  MQMessage message = new MQMessage();
                  try {
                      Queue.get(message, getOptions);
                      byte[] b = new byte[message.getMessageLength()];
                      message.readFully(b);
                      Object[] row = {count + 1, new String(b)};
                      publish(row);
                      message.clearMessage();
                      count++;
                      if (isCancelled()) {
                          modelMessages.setRowCount(0);
                          count = 0;
                          message.clearMessage();
                          return model;
                      }
                  }
                  catch (IOException e) {
                      break;
                  }
                  catch (MQException e) {
                      if (e.completionCode == 2 &amp;&amp; e.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE) {
                          break;
                      }
                  }
              }
              //modelMessages.fireTableDataChanged();
          } catch (MQException e) {
              txtMessage.setText("MQJE001: Completion Code '" + e.completionCode + "', Reason '" + e.reasonCode + "'.");
              modelMessages.setRowCount(0);
              modelMessages.fireTableDataChanged();
          }
          return model;
      }
  
      @Override
      protected void process(List&lt;Object[]&gt; chunks){
          for(Object[] row : chunks){
              model.addRow(row);
          }
      }
  
  }
  </code></pre>
  
  <p>And here is the listener:</p>
  
  <pre><code>    tableQueues = new JTable(modelQueues);
      tableQueues.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
              if (!e.getValueIsAdjusting()) {
                  txtMessage.setText("");
                  if (tableQueues.getSelectedRow() &gt; -1) {
                      gmw.cancel(false);
                      gmw = new GetMessagesWorker(modelMessages);
                      gmw.execute();
                  }
              }
          }
      });
  </code></pre>