i am doing some basic Java Swing application
(beginner level) .
what i have to do is when i press close button on JFrame
to colse the window i
You can do it by following steps:
Replace the line frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
with frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Implement WindowListener
and override its all abstract methods. You can find it here.
Override the public void windowClosing(WindowEvent e)
method some this way:
@Override
public void windowClosing(WindowEvent e){
int result = JOptionPane.showConfirmDialog(null, "Are you sure,"Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
System.exit(0);
}else{
//Do nothing
}
}