Popup button to close JFrame

I am doing a basic Java Swing application (entry level). what i need to do when i click close button on JFrame to open the window i want JOptionPane Confirm Dialog instead of just closing

here is the jframe code

  JFrame frame= new JFrame("frame"); frame.setSize(300,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); 

and the JOptionPane code is as follows

  final JOptionPane optionPane = new JOptionPane("Are You sure?",JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); 

therefore, when the Close button on the JFrame is pressed, this popup should appear instead of directly closing
Please help me how can I do this. thanks in advance

+4
source share
2 answers

You can do this by following these steps:

  • Replace the line frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  • Add a WindowListener and override all of its abstract methods. You can find it here .

  • Override the public void windowClosing(WindowEvent e) method as follows:

      @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 } } 
+3
source

Yes, you can do this using WindowListener .

  public void windowClosed(WindowEvent e) { //This will only be seen on standard output. displayMessage("WindowListener method called: windowClosed."); } public void windowOpened(WindowEvent e) { displayMessage("WindowListener method called: windowOpened."); } public void windowIconified(WindowEvent e) { displayMessage("WindowListener method called: windowIconified."); } public void windowDeiconified(WindowEvent e) { displayMessage("WindowListener method called: windowDeiconified."); } public void windowActivated(WindowEvent e) { displayMessage("WindowListener method called: windowActivated."); } public void windowDeactivated(WindowEvent e) { displayMessage("WindowListener method called: windowDeactivated."); } public void windowGainedFocus(WindowEvent e) { displayMessage("WindowFocusListener method called: windowGainedFocus."); } public void windowLostFocus(WindowEvent e) { displayMessage("WindowFocusListener method called: windowLostFocus."); } public void windowStateChanged(WindowEvent e) { displayStateMessage( "WindowStateListener method called: windowStateChanged.", e); 



See this tutorial for more details.

But for your scenario, I recommend that you work with the adapter class (since you only need one event, so you don’t need to get tired and apply all the methods), so here is an example according to your requirement

 import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; public class NoCloseFrame extends JFrame { public static void main( String[] arg ) { new NoCloseFrame(); } public NoCloseFrame() { super( "No Close Frame!" ); setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); setSize( 300, 300 ); setVisible( true ); addWindowListener( new AreYouSure() ); } private class AreYouSure extends WindowAdapter { public void windowClosing( WindowEvent e ) { int option = JOptionPane.showOptionDialog( NoCloseFrame.this, "Are you sure you want to quit?", "Exit Dialog", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null ); if( option == JOptionPane.YES_OPTION ) { System.exit( 0 ); } } } } 
+6
source

All Articles