Java AWT WindowFocusListener handle window focus event
import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import javax.swing.JFrame; class My implements WindowFocusListener { public void windowGainedFocus(WindowEvent e) { System.out.println("WindowFocusListener method called: windowGainedFocus."); }/*from w w w .j a v a 2s . c om*/ public void windowLostFocus(WindowEvent e) { System.out.println("WindowFocusListener method called: windowLostFocus."); } } public class Main { public static void main(String[] args) { JFrame window = new JFrame("java2s.com"); window.setBounds(30, 30, 300, 300); window.addWindowFocusListener(new My()); window.setVisible(true); } }