Search code examples
javapanels

Problem with imbricate panels in java


i have a panel in another panel and i want to access an member of the child panel from the parent panel. The child panel reference that is in the parent panel doesn't see all the members that it has. Thanks! PS : the members i can't access are public


Solution

  • I made a little test and it works, but on my project doesn't. I think i make a mistake somewhere. Here is the test:

    class Main
    {
      public static void main(String[] arg)
      {
        MainPanel mp = new MainPanel();
        mp.fct();
      }
    }
    
    class MainPanel extends Panel
    {
      SecondPanel sp;
      MainPanel()
      {
        sp = new SecondPanel();
      }
      void fct()
      {
        //the mainPanel can access member tf of second panel
        System.out.println(sp.tf.getText());
      }
    }
    
    class SecondPanel extends Panel
    {
      TextField tf;
      SecondPanel()
      {
        tf = new TextField("Abcde");
        this.add(tf);
      }
    }