0

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?

Jake Stanger
  • 449
  • 1
  • 8
  • 24
  • `I need to use the frame somewhere else` What this it means? a new instance? the same instance? Show some valid code(as short as possible) demonstrating your problem. – nachokk Jan 06 '14 at 14:14

1 Answers1

2

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);
}
Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254