Take the 2-minute tour ×

I am developing a Java Desktop Application with GUI implemented in SWING.

I hava a JFrame. I have added three JPanels on that. One JPanel panel1 has a Start Button. Now I want to disable various componets on other JPanels when a user presses the start button on the panel1.

Now how can I access the components of those other panels from panel1.

I know that one approach is to first get the container of panel1


Then get the components of the container


and use them as per need.

Q1. Is there any other way by which I can perform the same task? (I think this is the only way)

Q2. After getting the components list of the container, how to differentiate one container with other?

share|improve this question

4 Answers 4

up vote 1 down vote accepted

You can make instance variables that reference the panels when you create them, and use those variables to reference the panels.

public class myFrame extends JFrame {
   public static JPanel buttonPanel;
   public static JPanel statusPanel;

   public static void main(String[] args) {
      buttonPanel = new JPanel();   
share|improve this answer

I'd probably have a separate layer of the application -- one that holds references to the various panels and the start button -- handle this action. So, when the start button is clicked, it calls a method on some kind of Controller object; the Controller object, which has references to the other JPanels, disables the other components.

share|improve this answer

I would add an ActionListener from outside to the Start Button:

StartPanel panel1 = ...
JPanel panel2 = ....

panel1.getStartButton().addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent e) {
    setEnabledTree(panel2, false); 
share|improve this answer

I'd pass references to the other panels into the panel with the start button. Or simply have a method in the container which does exactly what you want and make a call to it.

share|improve this answer

Your Answer


