How do I handle JFrame window events?

Category: javax.swing, viewed: 6365 time(s).

This example show you how to handle JFrame window events such as windowOpened, windowClosing, windowClosed, etc. For handling these events we need to add a WindowListener listener to the JFrame instance. Here we use the WindowAdapter abstract class and implement the method which event we want to handle.

package org.kodejava.example.swing;

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

public class WindowListenerDemo extends JFrame {
    public WindowListenerDemo() {
        initializeComponent();
    }

    private void initializeComponent() {
        setSize(300, 300);
        setTitle("Window Listener");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.addWindowListener(new WindowAdapter() {
            //
            // Invoked when a window has been opened.
            //
            public void windowOpened(WindowEvent e) {
                System.out.println("Window Opened Event");
            }

            //
            // Invoked when a window is in the process of being closed.
            // The close operation can be overridden at this point.
            //
            public void windowClosing(WindowEvent e) {
                System.out.println("Window Closing Event");
            }

            //
            // Invoked when a window has been closed.
            //
            public void windowClosed(WindowEvent e) {
                System.out.println("Window Close Event");
            }

            //
            // Invoked when a window is iconified.
            //
            public void windowIconified(WindowEvent e) {
                System.out.println("Window Iconified Event");
            }

            //
            // Invoked when a window is de-iconified.
            //
            public void windowDeiconified(WindowEvent e) {
                System.out.println("Window Deiconified Event");
            }

            //
            // Invoked when a window is activated.
            //
            public void windowActivated(WindowEvent e) {
                System.out.println("Window Activated Event");
            }

            //
            // Invoked when a window is de-activated.
            //
            public void windowDeactivated(WindowEvent e) {
                System.out.println("Window Deactivated Event");
            }

            //
            // Invoked when a window state is changed.
            //
            public void windowStateChanged(WindowEvent e) {
                System.out.println("Window State Changed Event");
            }

            //
            // Invoked when the Window is set to be the focused Window, which means
            // that the Window, or one of its subcomponents, will receive keyboard
            // events.
            //
            public void windowGainedFocus(WindowEvent e) {
                System.out.println("Window Gained Focus Event");
            }

            //
            // Invoked when the Window is no longer the focused Window, which means
            // that keyboard events will no longer be delivered to the Window or any of
            // its subcomponents.
            //
            public void windowLostFocus(WindowEvent e) {
                System.out.println("Window Lost Focus Event");
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new WindowListenerDemo().setVisible(true);
            }
        });
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats