![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e609. Listening to All Focus Changes Between Components in an ApplicationTo listen to focus change events between components, install a listener with the keyboard focus manager. If you need the ability to veto (reject) a focus change, install a vetoable listener with the keyboard focus manager.KeyboardFocusManager.getCurrentKeyboardFocusManager() .addPropertyChangeListener(new FocusChangeListener()); KeyboardFocusManager.getCurrentKeyboardFocusManager() .addVetoableChangeListener(new FocusVetoableChangeListener()); 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) { // the newComp component gained the focus } else { // the oldComp component lost the focus } } else if ("focusedWindow".equals(evt.getPropertyName())) { if (oldComp == null) { // the newComp window gained the focus } else { // the oldComp window lost the focus } } } } 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) { // the newComp component will gain the focus } else { // the oldComp component will lose the focus } } else if ("focusedWindow".equals(evt.getPropertyName())) { if (oldComp == null) { // the newComp window will gain the focus } else { // the oldComp window will lose the focus } } boolean vetoFocusChange = false; if (vetoFocusChange) { throw new PropertyVetoException("message", evt); } }
e607. Preventing a Component from Gaining the Focus e608. Preventing a Window from Gaining the Focus e610. Setting Focus Traversal Keys in a Component e611. Setting Focus Traversal Keys for the Entire Application e612. Moving the Focus to the Next or Previous Focusable Component e613. Modifying the Focus Traversal Order e614. Setting the Initial Focused Component in a Window e615. Finding the Next Focusable Component e616. Determining If a Focus Lost Is Temporary or Permanent e617. Determining the Opposite Component of a Focus Event e618. Validating a JTextField When Permanently Losing the Focus e619. Removing the Focus from the Application e620. Activating a Keystroke When Any Component in the Window Has Focus e621. Activating a Keystroke When Any Child Component Has Focus
© 2002 Addison-Wesley. |