title: How can I reference or access objects such as buttons made in another method in Java Swing?
body: <p>I don't know if I am using the correct terminology in my question, sorry in advance if it sounds a bit confusing. So basically I am trying to write a Java Swing program to create a GUI. There are multiple components/objects that are more or less uniform throughout the GUI so I made separate methods to create different objects and panels and combined them together in the <code>master</code> panel that will be passed back to the <code>frame</code>.</p>
  
  <p>I laid out the entire GUI's layout and look and feel, but it is barely functional yet. My problem is that I have action listeners that need to reference or access buttons or objects that was made in and added to a panel in another method/class and passed back as a panel. For example,</p>
  
  <pre><code>    class makeComponents extends JPanel {
          public makeComponents() { 
  
          }
  
          public JToggleButton ToggleButton() {
            // Creates On/Off buttons as switches
              JToggleButton button = new JToggleButton("OFF", false);
              button.addActionListener(new ActionListener() {
                  AbstractButton abstractButton = (AbstractButton)e.getSource();
                  if (abstractButton.isSelected()) {
                      abstractButton.setText("ON");
                    // Want to add CardLayout card.show() to switch pictures
                  }
                  else {
                      abstractButton.setText("OFF");
                    // Want to add CardLayout card.show() to switch pictures
                  }
              }
              return button;
           }
       }
  </code></pre>
  
  <p>I have an on/off button like the above on a separate <code>left</code> JPanel then I have 2 images in CardLayout on <code>right</code> JPanel and the 2 panels are joined together in <code>master</code> JPanel which is passed back to the frame. I have created so many layers that I don't know how to backtrack and access that the pics so I can flip the CardLayout of the 2 images in the other panel.</p>
  
  <p>I hope I was clear enough and any help would be much appreciated. Thank you in advance!</p>