Skip to content
Advertisement

Can I acces variables and/or methods of the class a JFrame was created in from the panel?

shortened code:

public class Test{
  private JFrame frame;
  private TestPanel panel;
  
  Object obj;
 
  public static void main(String args[]) {
    Test test = new Test();
  }

  public Test() {
    try {
      // Setup GUI
      frame = new JFrame("Test");
      panel = new TestPanel();
      frame.add(panel);
    }
  }
}

Is it possible to directly access obj from panel? If yes: How?

Thank you in advance for your help. 🙂

Advertisement

Answer

No; Not without passing the reference to object to the panel.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement