Wednesday, July 3, 2024
HomeLanguagesJavaJava AWT | WindowStateListener

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 
     

Java




// 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();
    }
}


  1. Output: 
     

  1. Java Program to handle window events and identify them
     

Java




// 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 vertically");
 
        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 vertically");
 
        if (s1 == Frame.NORMAL)
            l1.setText("window normal");
    }
 
    // main class
    public static void main(String args[])
    {
        listener l = new listener();
    }
}


  1. Output: 
     

  1.  

  1.  

  1.  

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
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments