In this example we are going to see how to use JOptionPane ShowConfirmDialog.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* ............... START ............... */ import javax.swing.*; import java.awt.event.*; public class Main extends WindowAdapter { JFrame f; Main() { f = new JFrame(); f.addWindowListener(this); f.setSize(300, 300); f.setLayout(null); f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); f.setVisible(true); } public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(f, "Are you sure?"); if (a == JOptionPane.YES_OPTION) { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } public static void main(String[] args) { new Main(); } } /* ............... END ............... */ |
Output:
