0

I'm a beginner in Java, coming from VB.net. Here's what I'm trying to do using Netbeans:

I've created 2 JFrame classes: JFrame1 and JFrame2. The first one is instantiated from the main class of the project.

There is a button on JFrame1. When I click it, a new instance of JFrame2 is created and shown, and the already created instance of JFrame1 is hidden. With another button on the JFrame2, how do I make the already created JFrame1 visible again without creating a new instance. Below is an excerpt of the code I would like to implement.

JFrame1 :

public class JFrame1 {

 private void button1(java.awt.event.ActionEvent evt){
   JFrame2 jf2 = new JFrame2();
   jf2.setVisible(true);
   this.setVisible(false);
}
}

JFrame2 :

public class JFrame2 {

 private void button2(java.awt.event.ActionEvent evt){
  this.setVisible(false);

  // here the line that would make Jframe1 visible without recreating an instance.
}
}

One last request thing. How can I destroy JFrame2 to free memory space?

Robert Martin
  • 1,585
  • 1
  • 13
  • 20
Kurt Miller
  • 567
  • 1
  • 8
  • 23
  • [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)? You might like to take a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) and [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for an alternative – MadProgrammer Mar 03 '16 at 20:46
  • Thanks buddy I'll do that – Kurt Miller Mar 04 '16 at 22:46

2 Answers2

1

You could pass a reference of the first frame to the constructor of the second frame. The second frame stores this reference in a private member.
Like this:

public class JFrame2
{
  public JFrame2(JFrame1 owner)
  {
    jf1= owner;
  }

  private JFrame1 jf1;

} // class JFrame2

Then you call within the first frame:

Jframe2 jf2 = new Jframe2(this);

When the button within the second frame is pressed, you call jf1.setVisible(true).

In order to destroy the second frame, try fr2.setDefaultCloseOperation(DISPOSE_ON_CLOSE)

Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • You could pass a reference of the first frame to the constructor of the second frame. How do I do that please (I'm new to Java and Object oriente programming) – Kurt Miller Mar 03 '16 at 14:06
1

Just pass a handle of JFrame1 object to JFrame2.

JFrame1 :

public class JFrame1 {    
 private void button1(java.awt.event.ActionEvent evt){
   JFrame2 jf2 = new JFrame2(this);
   jf2.setVisible(true);
   this.setVisible(false);
 }
}

JFrame2 :

public class JFrame2 {
 private JFrame1 frame1;

 public JFrame2(JFrame1 frame1) {
  this.frame1 = frame1;
 }

 private void button2(java.awt.event.ActionEvent evt){
  this.setVisible(false);

  frame1.setVisible(true);
 }
}
vsimkus
  • 335
  • 1
  • 10
  • It's not working. I think I am missing something. All the lines were written as you said. But the second JFrame2 doesn't show properly. Some "bugging" window appears but none of the element appears on the form. So when I close that "buggy" window, the Frame1 does not show. Could you tell me what you think I might be missing here ? – Kurt Miller Mar 03 '16 at 14:31
  • RESOLVED !!! Actually Netbeans creates a constructor by default for jFrames. And I wrote a second constructor method for Jframe2. So there was the problem. After that there was another issue ; the main () method of Jframe2 need to be passed an argument. There I only chose to pass ''null'' as argument. And there ya go. I worked as expected. Thank you. – Kurt Miller Mar 03 '16 at 14:55
  • While it will work, this is actually not recommended because you expose the entire JFrame which is not necessary. – user3437460 Mar 03 '16 at 15:42