title: Update a Java Swing JList
body: <p>I used <em>WindowBuilder Pro</em> to generate most of the code in the <em>GuiTest</em> Class below with the exception of lines with '////////' to the right which I added (or altered). WindowBuilder Pro (a WYSIWYG) keeps adding code to the constructor - but has not complained yet with me making alterations. I want to keep building with WindowBuilder Pro in this manner, so <em>I don't want to make changes to the general architecture of the GUI</em>. </p>
  
  <p>I also created a DataTest class further below to emulate a certain slow process of gathering a data set. </p>
  
  <p><strong>Here's my question:</strong>
  What I would like to do is update the JList with the new data from the DataTest class.</p>
  
  <p>GuiTest Class:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.EventQueue;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.ListSelectionModel;
  import javax.swing.border.EmptyBorder;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener; ///////////
  import javax.swing.JList;
  import javax.swing.JLabel;
  
  import discoverTool.DataTest;
  
  public class GuiTest extends JFrame implements ListSelectionListener{
      private String[] foo =  {"thing1","thing2","thing3"}; //////////////
  
      private JPanel contentPane;
      private JLabel lblNewLabel; //////////////made global
  
      /**
       * Launch the application.
       * @throws InterruptedException 
       */
      public static void main(String[] args) throws InterruptedException {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      GuiTest frame = new GuiTest();
                      frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
          DataTest dt = new DataTest();/////////////
          GuiTest gt = new GuiTest();
          gt.foo = dt.foo;
          gt.updateJList(dt.foo);
      }
  
      public void updateJList(String[] f){
          //reset the list with f
      }
  
      /**
       * Create the frame.
       */
      public GuiTest() {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(100, 100, 450, 300);
          contentPane = new JPanel();
          contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
          setContentPane(contentPane);
          contentPane.setLayout(null);
  
          JList list = new JList(foo);
          list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); /////////////
          list.setSelectedIndex(0);               //////////////
          list.addListSelectionListener(this);    //////////////      
          list.setBounds(10, 11, 134, 240);
          contentPane.add(list);
  
          JPanel panel = new JPanel();
          panel.setBounds(154, 11, 188, 81);
          contentPane.add(panel);
          panel.setLayout(null);
  
          //JLabel lblNewLabel = new JLabel("New label");/////////
          lblNewLabel = new JLabel("New label");///////////
          lblNewLabel.setBounds(10, 11, 46, 14);
          panel.add(lblNewLabel);
      }
  
      public void valueChanged(ListSelectionEvent e) {///////////////
          JList jList = (JList)e.getSource();////////////
          lblNewLabel.setText( foo[jList.getSelectedIndex()] );///////////
      }///////////
  }
  </code></pre>
  
  <p>DataTest class:</p>
  
  <pre><code>public class DataTest {
  
      public String[] foo;
  
      public DataTest() throws InterruptedException{
          //simulate a long process getting data;
          Thread.sleep(7000);
          foo = new String[4];
          foo[0]="hey";
          foo[1]="hi";
          foo[2]="bye";
          foo[3]="adios";
      }
  }
  </code></pre>