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:
- 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
Recommended Posts:
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
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.