How to use extended class as variable?

141 views Asked by At

My problem is hopefully simple to solve and I believe is just a lack of my Java knowledge. I've probably used the wrong terminology in the title. I'm sorry about that. Basically, I'm using Java swing and my frame is a class (public className extends JFrame). I need to use the frame somewhere else. How do I do that? I have tried using JFrame frame = main.Window but have been told it cannot be resolved to a variable. So how do I use it?

1

There are 1 answers

0
Duncan Jones On BEST ANSWER

Firstly, don't extend JFrame. What you probably need to do is have a field in your class which is a JFrame.

To provide access to this field from another class, add a getter method to your class and ensure you pass a reference to your class to the other class. E.g.

public class Name {
  private final JFrame frame = new JFrame();

  public JFrame getFrame() {
    return frame;
  }
}

Alternatively, decide what other classes may wish to do with your frame and provide specific methods in your class to support this. e.g.

public void makeFrameVisible() {
  frame.setVisible(true);
}