JFrame Window Event
It's possible to add JFrame Window Event or any other top swing window like JPanel etc.WindowAdapter class has an empty implementation of all WindowListener interface methods.
Example of JFrame Window Event Listening
[
import javax.swing.JFrame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; public class JFrameWindowEventsExample { public static void main(String args[]) { JFrame frame=new JFrame("Window Events Example"); frame.addWindowListener(new WindowListener() { public void windowOpened(WindowEvent evt) { System.out.println("Window opened"); } public void windowClosed(WindowEvent evt) { System.out.println("Window closed"); } public void windowClosing(WindowEvent evt) { System.out.println("Window closing"); } public void windowIconified(WindowEvent e) { System.out.println("JFrame is minimized."); } @Override public void windowDeiconified(WindowEvent e) { System.out.println("JFrame is restored."); } @Override public void windowActivated(WindowEvent e) { System.out.println("JFrame is activated."); } @Override public void windowDeactivated(WindowEvent e) { System.out.println("JFrame is deactivated."); } }); // Use the WindowAdapter class to intercept only the window closing event frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("JFrame is closing."); } }); frame.setSize(400,430); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
]