WindowEvent is a part of the java.awt.event package. It is triggered when a window changes its state, such as opening, closing, minimizing, or restoring.
Example:
Source Code:import javax.swing.JOptionPane;
import javax.swing.JFrame;
private void formWindowOpened(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Opened");
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Closing...");
int option = JOptionPane.showConfirmDialog(this, "Are you sure to exit?",
"Confirm Exit", JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else {
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
private void formWindowClosed(java.awt.event.WindowEvent evt) {
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Activated");
}
private void formWindowDeactivated(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Deactivated");
}
private void formWindowIconified(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Minimized");
}
private void formWindowDeiconified(java.awt.event.WindowEvent evt) {
labelStatus.setText("Window Restored");
}
Result:
Watch the video:Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

