title: Java: Add Elements to a Panel from an different method
body: <p>I created a Window with the Windowbuilder of eclipse. the window contains a content panel and two scroll panels within the content panel. I want to add elements to the two scroll panels out of a different method. My code looks like this(just the relevant parts): </p>
  
  <pre><code>public window() {
    contentPane = new JPanel(); // Plus some methods like setLayout or setBorder for the    contentpane
  
     JScrollPane scrollPane1 = new JScrollPane();
     contentPane.add(scrollPane1);  
  
     JScrollPane scrollPane2 = new JScrollPane();
     contentPane.add(scrollPane2);  
  }
  
  public static void addItems(ArrayList&lt;String&gt; list)
  {
      Window w = new Window();
  
      for(String s : list)
      {
         w.contentPane.scrollPane1.addElement(s);
      /* Normally it should be something like this, but I just get access 
      to the contentPane and cannot add anything directly to the ScrollPanes. */      
      }
  }
  </code></pre>
  
  <p>Is there any special setting that denies to access the single components directly? </p>
  
  <p>edit: Thanks to @summerbulb I made some changes to the <code>addItems</code>-Method, it now looks like this.</p>
  
  <pre><code>    public static void addItems(ArrayList&lt;String&gt; appList)
  {
      WindowAppsAndHardware w = new WindowAppsAndHardware();
      Component[] components = w.contentPane.getComponents(); 
      Component component = null; 
  
      for(String s : appList)
      {
      for (int i = 0; i &lt; components.length; i++) 
      { 
         component = components[i]; 
         if (component.getName().equals("scrollPane1")); 
         { 
           Label lbl = new Label();
           lbl.setName(s);
           component.addElement(lbl); 
           /*Here I want to add the Label to the component,
           but component dont have the `addElement`-Method.*/
         } 
      }
      }
  }
  </code></pre>