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:
- Java Program to handle window events
// 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_rightfilter_noneOutput:
- Java Program to handle window events and identify them:
// 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_rightfilter_noneOutput:
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.