implements VetoableChangeListener to block focus change events : VetoableChangeListener « Swing Event « Java Tutorial






import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

public class Main {
  public static void main(String[] argv) {
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener(
        new FocusVetoableChangeListener());
  }
}

class FocusVetoableChangeListener implements VetoableChangeListener {
  public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
    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());
      }
    }

    boolean vetoFocusChange = false;
    if (vetoFocusChange) {
      throw new PropertyVetoException("message", evt);
    }
  }
}








15.40.VetoableChangeListener
15.40.1.Listening to JComponent Events with a VetoableChangeListenerListening to JComponent Events with a VetoableChangeListener
15.40.2.implements VetoableChangeListener to block focus change events