Java AWT | WindowStateListener

WindowStateListener is a part of java.awt.event package. It is an interface to handle window state events. This interface returns an event when the frame or window associated with it is iconified or maximized.

Abstract function of the class:

  • windowStateChanged(WindowEvent e) : invoked when a state of the window is changed.

Below programs illustrate the WindowStateListener:



  1. Java Program to handle window events
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to handle window events
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class listener1 extends JFrame implements WindowStateListener {
        listener1()
        {
            super("main frame");
            setSize(500, 500);
      
            // create a sub frame
            JFrame f = new JFrame("sub");
      
            // add window state listener
            f.addWindowStateListener(this);
      
            // set size of window
            f.setSize(200, 200);
      
            show();
            f.show();
        }
      
        // if state of window is changed
        public void windowStateChanged(WindowEvent e)
        {
            JOptionPane.showMessageDialog(this, "window state changed");
        }
        // main class
        public static void main(String args[])
        {
            listener1 l = new listener1();
        }
    }

    chevron_right

    
    

    Output:

  2. Java Program to handle window events and identify them:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to handle window events and identify them
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class listener extends JFrame implements WindowStateListener {
        // label
        JLabel l;
        JLabel l1;
      
        listener()
        {
            super("main frame");
            setSize(500, 500);
      
            // create a sub frame
            JFrame f = new JFrame("sub");
      
            // add window state listener
            f.addWindowStateListener(this);
      
            // set size of window
            f.setSize(200, 200);
      
            // create a label
            JLabel lo = new JLabel("old state : ");
            JLabel lo1 = new JLabel("new state : ");
      
            l = new JLabel("");
            l1 = new JLabel("");
      
            // create a panel
            JPanel p = new JPanel();
      
            p.add(lo);
            p.add(l);
            p.add(lo1);
            p.add(l1);
            add(p);
      
            show();
            f.show();
        }
      
        // if state of window is changed
        public void windowStateChanged(WindowEvent e)
        {
            int s = e.getOldState(), s1 = e.getNewState();
      
            // for old state
            if (s == Frame.ICONIFIED)
                l.setText("window iconified");
      
            if (s == Frame.MAXIMIZED_BOTH)
                l.setText("window maximized horizontally and vertically");
      
            if (s == Frame.MAXIMIZED_HORIZ)
                l.setText("window maximized horizontally");
      
            if (s == Frame.MAXIMIZED_VERT)
                l.setText("window maximized verically");
      
            if (s == Frame.NORMAL)
                l.setText("window normal");
      
            // for new state
      
            if (s1 == Frame.ICONIFIED)
                l1.setText("window iconified");
      
            if (s1 == Frame.MAXIMIZED_BOTH)
                l1.setText("window maximized horizontally and vertically");
      
            if (s1 == Frame.MAXIMIZED_HORIZ)
                l1.setText("window maximized horizontally");
      
            if (s1 == Frame.MAXIMIZED_VERT)
                l1.setText("window maximized verically");
      
            if (s1 == Frame.NORMAL)
                l1.setText("window normal");
        }
      
        // main class
        public static void main(String args[])
        {
            listener l = new listener();
        }
    }

    chevron_right

    
    

    Output:

Note : The following program will not run in an online compiler please use an offline IDE

Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowStateListener.html



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.