java - How to close current JFrame? -
i have read similar topics did find answer there.
i created jframe
close button. after click want close current window. try setdefaultcloseoperation(jframe.hide_on_close)
, or setvisible(false)
.
public class windows { jframe frame; jframe frame1; public windows(){ } public jframe getcreateframe(){ frame1 = new jframe("create user"); frame1.setsize(500,500); frame1.setvisible(true); frame1.getcontentpane().add(new panels().getwelcomtxtlabelpanel1(), borderlayout.north); frame1.getcontentpane().add(new panels().getcreateuser(), borderlayout.south); frame1.getcontentpane().add(new panels().getuserlabel(), borderlayout.west); frame1.getcontentpane().add(new panels().getuserfield(), borderlayout.center); return frame1; } }
here button.
public jbutton getcancelbutton(){ cancel = new jbutton("cancel"); cancel.addactionlistener(new actionlistener() { public void actionperformed(actionevent e){ new windows().getcreateframe().setdefaultcloseoperation(jframe.hide_on_close); } }); return cancel; }
the problem following action (and not this):
cancel.addactionlistener(new actionlistener() { public void actionperformed(actionevent e){ new windows().getcreateframe().setdefaultcloseoperation(jframe.hide_on_close); } });
here create new windows
object , call getcreateframe()
creates new jframe
, call setdefaultcloseoperation()
on it.
so you work different windows
/ jframe
instances.
instead should create jframe
in constructor of windows
, call setdefaultcloseoperation(jframe.hide_on_close)
of jframe
in constructor well.
afterwards can use setvisible(false)
in action - jframe
, not new created one.
btw. getcancelbutton()
should not create new button every time called.
Comments
Post a Comment