KeyboardFocusManager: addPropertyChangeListener(PropertyChangeListener listener) : KeyboardFocusManager « java.awt « Java by API






KeyboardFocusManager: addPropertyChangeListener(PropertyChangeListener listener)

 

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());
      }
    }
  }
}

   
  








Related examples in the same category

1.KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS
2.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3.KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
4.KeyboardFocusManager: clearGlobalFocusOwner()
5.KeyboardFocusManager: focusNextComponent()
6.KeyboardFocusManager: getFocusOwner()
7.KeyboardFocusManager: getFocusedWindow()