title: Java Swing: Access panel components from another class
body: <p>Hi i basically have two classes, 
  one main and one just to separate the panels, just for code readability really.</p>
  
  <p>i have :</p>
  
  <pre><code>public class Main{
  
     public static void main (String args[]) {
      JFrame mainJFrame;
      mainJFrame = new JFrame();
  
      //some other code here
  
      CenterPanel centerPanel = new CenterPanel();
      centerPanel.renderPanel();
      mainFrame.add(centerPanel.getGUI());
  
     }
  
  
  }
  
  class CenterPanel{
      JPanel center = new JPanel();
  
      public void renderPanel(){
          JButton enterButton = new JButton("enter");
          JButton exitButton = new JButton("exit");
          center.add(exitButton);
          center.add(enterButton);
      }
  
      public JComponent getGUI(){
          return center;
      }
  }
  </code></pre>
  
  <p>Above code works perfectly. It renders the centerPanel which contains the buttons enter and exit. My question is:</p>
  
  <p>I still need to <strong>manipulate the buttons</strong> in the main, like change color, add some action listener and the likes. But i <strong>cannot access them anymore</strong> in the <strong>main</strong> because technically they are from a different class, and therefore in the main, centerPanel is another object.</p>
  
  <p>How do I access the buttons and use it (sets, actionlisteners, etc)? even if they came from another class and i still wish to use it inside the main?Many Thanks!</p>