Reputation: 3240
I have following scenario (2 classes)
Frame1(next button)
Frame2(prev button, close button)
Frame1 opened (instance 1)
to go Frame1 - Frame2 (frame1.setVisible(false)
and frame2.setVisible(true)
) (instance 2)
to go Frame2 - Frame1 (frame2.setVisible(false)
and frame1.setVisible(true)
) (instance 3)
again Frame1 - Frame2 (frame1.setVisible(false)
and frame2.setVisible(true)
) (instance 4)
in above process 4 instances (If program opened then noted as instance)
now during close from Frame2, I m using frame1.dispose()
and frame2.dispose()
here only 2 times instances are disposed.
But the problem is: still 2 instances are not disposed.
But only only one process is an alive. How is it possible?
In above scenario should I dispose all instances to kill the process ?
Upvotes: 1
Views: 1592
Reputation: 9178
You consider to do like this if you do not want to go with CardLayout
Create a constructor in Frame1
with argument of Frame2
as shown below and store frame2 object locally.
public class Frame1{
private Frame2 frame2Obj;
public Frame1(Frame2 frame1Obj){
this.frame2Obj = frame2Obj;
}
private openFrame1(){
frame2Obj.setVisible(true);
this.setVisible(false);
}
}
Similarly create a constructor in Frame2
with argument of Frame1
as shown below and store frame1 object locally.
public class Frame2{
private Frame1 frame1Obj;
public Frame2(Frame1 frame1Obj){
this.frame1Obj = frame1Obj;
}
private openFrame1(){
frame1Obj.setVisible(true);
this.setVisible(false);
}
}
Now you will have only two instances.
Upvotes: 1
Reputation: 109813
default value DefaultCloseOperation
for JFrame
is HIDE_ON_CLOSE
have to set DefautlCloseOperation
for JFrame
with proper value (EXIT_ON_CLOSE)
don't create an second or more JFrames
, use CardLayout
instead
use JDialog
instead another JFrame
, in the case that there are real reasons
Upvotes: 1