How to launch a pop up when a user clicks close on a Java Swing application -
this question has answer here:
i have jframe has setdefaultcloseoperation(jframe.exit_on_close);
is possible me change when user clicks close launch "are sure?" joptionpane style pop up?
how can this?
you need add windowlistener. here's example:
public class test { public static void main(string[] args) { final jframe frame = new jframe(); jlabel label = new jlabel("test"); frame.getcontentpane().add(label); frame.pack(); frame.setdefaultcloseoperation(windowconstants.do_nothing_on_close); frame.addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent ev) { int result = joptionpane.showconfirmdialog(frame, "sure?"); if (result == joptionpane.ok_option) { frame.dispose(); } } }); frame.setvisible(true); } }
Comments
Post a Comment