How to capture a JFrame's close button click event?

java take screenshot of active window
java screen capture
jframe tutorial
jframe example
jframe tutorial for beginners
jframe example with buttons
import jframe
jframe methods

I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked.

How can I capture that event?

I'd also like to prevent the window from closing if the user chooses not to proceed.

import javax.swing.JOptionPane;
import javax.swing.JFrame;

/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        if (JOptionPane.showConfirmDialog(frame, 
            "Are you sure you want to close this window?", "Close Window?", 
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){

If you also want to prevent the window from closing unless the user chooses 'Yes', you can add:


Java prog#101. How to Capture or save image of JFrame ,JPanel , Java prog#101. How to Capture or save image of JFrame ,JPanel and swing Component part Duration: 5:45 Posted: 23 Sep 2012 Heys guys, I've developped a code to take a screen shot of my whole screen, but I want it to take the screen shot of only the things inside my Jframe. Ill be using it to print the image later on by the way. And one of the main problem is, the mouse also comes inside the snapshot. I don't want the mouse or the two buttons at the bottom.

Override windowClosing Method.

public void windowClosing(WindowEvent e)

It is invoked when a window is in the process of being closed. The close operation can be overridden at this point.

Capture System.out into a JFrame, Capture System.out into a JFrameTag(s): Swing. import java.awt.*; import javax. swing. A JFrame is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. We can add the components to a JFrame to use its contentPane instead. A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame.


also works. First create a JFrame called frame, then add this code underneath.

How to Make Frames (Main Windows) (The Java™ Tutorials , A frame, implemented as an instance of the JFrame class, is a window that has int getDefaultCloseOperation(), Set or get the operation that occurs when the� JFrame class has many methods which can be used to customize it. Creating a JFrame. JFrame class has many constructors used to create a JFrame. Following is the description. JFrame(): creates a frame which is invisible. JFrame(GraphicsConfiguration gc): creates a frame with a blank title and graphics configuration of screen device.

This may work:

jdialog.addWindowListener(new WindowAdapter() {
    public void windowClosed(WindowEvent e) {
        System.out.println("jdialog window closed event received");

    public void windowClosing(WindowEvent e) {
        System.out.println("jdialog window closing event received");


Java Capture/save image from swing Component eg : JFrame JPanel, Java Capture/save image from swing Component eg : JFrame JPanel .. Capture image from any swing Component and save to file. Capture : BufferedImage� JFrame Java Tutorial What is JFrame ? It is an extended version of Java.awt.Frame that adds support for the JFC/Swing architecture .; JFrame is the core class of javax.swing package and is used to develop GUI (graphical user interface ) in which various visual objects like Text Field,Radio Button,scroll Bar,check Box etc are embedded.

This is what I put as a menu option where I made a button on a JFrame to display another JFrame. I wanted only the new frame to be visible, and not to destroy the one behind it. I initially hid the first JFrame, while the new one became visible. Upon closing of the new JFrame, I disposed of it followed by an action of making the old one visible again.

Note: The following code expands off of Ravinda's answer and ng is a JButton:

ng.addActionListener((ActionEvent e) -> {
    JFrame j = new JFrame("NAME");
    j.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {

How do I get the JFrame of a component?, In this example we try to get the JFrame from a button action listener event. To get the JFrame we utilize the SwingUtilities.getRoot() method and� With JDK 7, we can create a gradient based translucent window using swing very easily. Following are the steps needed to make a gradient-based translucent window.

How to open a webcam in a new jframe and dispose calling jframe , Issue #587 � sarxos/webcam-capture � GitHub. Skip to content. Sign up. 2. Make a JFrame Form and design it. 3. RC on a button and Goto Events~action~actionPerformed 4. Write following code in it. Note: createScreenCapture(Rectangle rect) method is used to capture screen.

Get JFrame window size information in Java, To get JFrame window size information, you can use the following −environment .getMaximumWindowBounds();For Screen Size −config. This tutorial explains how you can interact with JFrame by using buttons, listeners and text fields.. Background . Interactivity is what user wants in every application. To add interactivity in a program, Java provides us a very easy way.

How to center a JFrame on screen, public ImageRotatorMain() { // create a new jframe, and pack it frame Java screenshots: How to take a screen capture with Java and Swing� To add background image to JFrame, use the getImage() method of the Image class − Image img = Toolkit.getDefaultToolkit().getImage("E:\\rahul.jpg"); Now, draw the image − public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); } The following is an example to add Background Image to JFrame − Example

  • Will this work if the user closes the application (for example, by hitting Cmd + Q on a Mac)? Does the class that implements this method have to declare implements WindowListener for this code to work? If so, it might be good to include that in the answer. Otherwise this is an elegant solution.
  • @brandaemon If you add this line of code to the top of your main() method "System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");" then it will call the windowListener when Cmd + Q is pressed on mac.
  • But this does not execute the confirmExit() method as the OP would like it
  • It does the same thing though
  • No, I don't want to simply close the program. I want to do something before it happens.
  • oh sorry, I didn't know that.
  • "What I want to do is to call ... confirmExit() when [the X] is clicked." And the previous comment is unrelated to OQ.