title: How to dynamicly add elemnts to a JList / DefaultListModel
body: <p>I'm want to dynamic add elements to my <code>DefaultListModel</code> / <code>JList</code> but the list needs to emptied first. I do this opening an dialog-window . My problem is that when I use <code>model.removeAllElements()</code> my dialog-window reappears multiple times. What am i doing wrong?</p>
  
  <p>I also tried <code>model.addElementAt(index)</code> to bypass <code>model.removeAllElements()</code> but the result is the same. </p>
  
  <pre><code>private javax.swing.JList serviceList;
  serviceList.setModel(model);
  serviceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  serviceList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
  serviceList.setSelectionBackground(java.awt.Color.white);
  serviceList.setVisibleRowCount(3);
  serviceList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
          public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
              serviceListValueChanged(evt);
          }
      });
  
   private void serviceListValueChanged(javax.swing.event.ListSelectionEvent evt) {                                         
      showTasksDialog();
  }
  </code></pre>
  
  <p><code>showTasksDialog()</code>: open a dialog-window with 3 buttons when the user clicks the first one it connects to a URL then the List is updated by <code>filllst()</code>.</p>
  
  <pre><code>public void showTasksDialog() {
      int selection = serviceList.getSelectedIndex();
      Object[] options = {"Analyse", "Build", "Stop"};
      int n = taskDialog.showOptionDialog(this,
              "What should this Service do?",
              "",
              JOptionPane.YES_NO_CANCEL_OPTION,
              JOptionPane.QUESTION_MESSAGE,
              null,
              options,
              null);
      if (n == 0) {
          try {
              connection.setSlaveToAnalyse(serviceURLJSONArray.getString(selection));
              filllist();
          } catch (JSONException | IOException ex) {
              Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p><code>filllist()</code>: should remove all elements from my default list and the refill it, but if I use <code>model.removeAllElements()</code> the Dialog-windows reappears multiple times. When I don't use <code>removeAllElements</code> then everything is fine but the list is not emptied</p>
  
  <pre><code>public void filllist() throws JSONException, IOException {        
      model.removeAllElements();
      serviceURLJSONArray = connection.getSlaves();
      for (int i = 0; i &lt; serviceURLJSONArray.length(); i++) {
          String slaveStatus = new Connection().getSlaveStatus(serviceURLJSONArray.getString(i));
          model.addElement("Service " +(i+1)+" "+slaveStatus);
      }
  }
  </code></pre>