import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Component; import java.awt.Container; public class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { JButton aButton = (JButton) e.getSource(); MyFrame aFrame = (MyFrame) aButton.getRootPane().getParent(); JLabel aLabel1 = aFrame.getLabel1(); JLabel aLabel2 = aFrame.getLabel2(); // A JFrame actually contains just one GUI component, the content pane. // GUI widgets that appear to be added to the JFrame are actually added to the // content pane (a container in and of itself). Get the components inside the // content pane to actually get the widgets that appeared to be added to // the JFrame. Container aContainer = aFrame.getContentPane(); Component aComponent = aContainer.getComponent(0); // First item added to array if (aComponent instanceof JLabel) { aLabel1 = (JLabel) aComponent; aLabel1.setText("Effect1"); } aComponent = aContainer.getComponent(1); // Second item added to array if (aComponent instanceof JLabel) { aLabel2 = (JLabel) aComponent; aLabel2.setText("Effect1"); } } }