Confirmation message on click in 'X' button of a JFrame
 01 September 2017
 Java | Swing
 Rodrigo Costa Corrêa

To show a confirmation message to the user if he clicks the "X" button of JFrame, simply add the code below to the JFrame of your project.

You need to customize with the message you want to appear and need to configure exactly what will happen if the user clicks the "X" button. Usually I call a function to close the connection to the database and put the code to close the program. But that's up to everyone.

Code to be added to the JFrame:

YOUR_JFRAME.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				if (OptionPaneType.showYesNo("Do you really want to close the program?") == JOptionPane.YES_OPTION) {
					dataBase.closeConnection(); // Close the Database connection
					System.exit(0); // Close the program
				}
			}
		});