Java tutorial
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main extends JFrame implements WindowFocusListener { static Main frame = new Main("WindowEventDemo"); JTextArea display; public static void main(String[] args) { frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addComponentsToPane(); frame.pack(); frame.setVisible(true); } private void addComponentsToPane() { display = new JTextArea(); display.setEditable(false); JScrollPane scrollPane = new JScrollPane(display); scrollPane.setPreferredSize(new Dimension(500, 450)); getContentPane().add(scrollPane, BorderLayout.CENTER); addWindowFocusListener(this); } public Main(String name) { super(name); } public void windowGainedFocus(WindowEvent e) { displayMessage("WindowFocusListener method called: windowGainedFocus."); } public void windowLostFocus(WindowEvent e) { displayMessage("WindowFocusListener method called: windowLostFocus."); } private void displayMessage(String msg) { display.append(msg + "\n"); System.out.println(msg); } }