Reputation: 113
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?
Upvotes: 0
Views: 95
Reputation: 4534
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.
Upvotes: 2