Question

i have design two JFrames in netbeans(new-> Jframe ) and add one botton on each jframe , when i click a button from frame1 it opens Jframe2 its okay but i want is that the JFrame1 should be closed or not appear on the window..and alternatively when i click button from jframe2 it opens jframe1 but jframe2 should be closed..the one thing i achieved is jframes are opening on buttons click but jframes are not closing as per my requirements.. so i need expertise from you guys to fix this problem.

Was it helpful?

Solution

In your button event handler (assuming your class extends JFrame), do something like this before opening the new frame: this.dispose()

From the Java doc, the .dispose() method:

public void 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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top