tags:

views:

79

answers:

4

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

panel1.getParent();

Then get the components of the container

container.getComponents();

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?

A: 

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.

denis
+1  A: 

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.

Jim Kiley
+1  A: 

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); 
  }
}
ZeissS
+1  A: 

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();   
   }
}
Marcus Adams