Event Java

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class Main {
  public static void main(String[] argv) {
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(
        new FocusChangeListener());
  }
}
class FocusChangeListener implements PropertyChangeListener {
  public void propertyChange(PropertyChangeEvent evt) {
    Component oldComp = (Component) evt.getOldValue();
    Component newComp = (Component) evt.getNewValue();
    if ("focusOwner".equals(evt.getPropertyName())) {
      if (oldComp == null) {
        System.out.println(newComp.getName());
      } else {
        System.out.println(oldComp.getName());
      }
    } else if ("focusedWindow".equals(evt.getPropertyName())) {
      if (oldComp == null) {
        System.out.println(newComp.getName());
      } else {
        System.out.println(oldComp.getName());
      }
    }
  }
}