0

I have a main JFrame which has other JFrames embedded to it which opens up on clicking the buttons on the main JFrame. The embedded JFrame has the following codes to close:

 public void windowClosing(WindowEvent we)
{
System.exit(0);
}

but the problem is once i close the embedded JFrame the main JFrame also closes with it. I want only the embedded JFrame to close. can anyone help me out with this? Secondly, is

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

or

   public void windowClosing(WindowEvent we)
  {
    System.exit(0);
  }

better to close a frame?

user3320152
  • 113
  • 2
  • 7
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 18 '14 at 07:15
  • thanks! JInternalFrame would be the best to implement in this case? – user3320152 Feb 18 '14 at 07:22
  • Well, 'it depends' on what this app. is! What is it, and what is it currently putting in the multiple frames? Different documents for view/edit? Different movies? Different news feeds? .. – Andrew Thompson Feb 18 '14 at 07:28
  • Well, I am trying to make an app where socket programming is being used. I am trying to embed other Frames which include chat, voice call, file sharing and remote desktop into the main frame. – user3320152 Feb 18 '14 at 07:34

1 Answers1

2

For closing embedded JFrame use dispose() function.

 public void windowClosing(WindowEvent we)
{
    dispose();
}

OR use

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

Note:

When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.

AJ.
  • 4,526
  • 5
  • 29
  • 41