Give Yes / No option in Java GUI

I would like to modify the code below to present a yes or no option when the user presses X, but I am afraid that my java newbie skills are not yet drawn to it. Any suggestions please? I would like to leave the code below as complete as possible to see what needs to be done differently for future reference.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class WindowExit extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
    JOptionPane.showMessageDialog( null, "Are you sure you want to close?" );
    System.exit(0);
}
}
+3
source share
3 answers

Use showConfirmDialog as follows:

int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", "Close?",  JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
   System.exit(0);
}
+11
source

Check out the docs . There JOptionPane.YES_NO_OPTIONis which you can pass as a parameter.

+6
source
public static int showConfirmDialog(Component parentComponent,
                                    Object message,
                                    String title,
                                    int optionType)

optionType of JOptionPane.YES_NO_OPTION

+2

All Articles