Package org.jdesktop.swingx.event
Class CompoundFocusListener
- java.lang.Object
-
- org.jdesktop.beans.AbstractBean
-
- org.jdesktop.swingx.event.CompoundFocusListener
-
public class CompoundFocusListener extends AbstractBean
An convenience class which maps focusEvents received from a container hierarchy to a bound read-only property. Registered PropertyChangeListeners are notified if the focus is transfered into/out of the hierarchy of a given root.F.i, client code which wants to get notified if focus enters/exits the hierarchy below panel would install the compound focus listener like:
// add some components inside panel.add(new JTextField("something to .... focus")); panel.add(new JXDatePicker(new Date())); JComboBox combo = new JComboBox(new Object[] {"dooooooooo", 1, 2, 3, 4 }); combo.setEditable(true); panel.add(new JButton("something else to ... focus")); panel.add(combo); panel.setBorder(new TitledBorder("has focus dispatcher")); // register the compound dispatcher CompoundFocusListener report = new CompoundFocusListener(panel); PropertyChangeListener l = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { // do something useful here }}; report.addPropertyChangeListener(l);
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
focused
private java.beans.PropertyChangeListener
managerListener
PropertyChangeListener registered with the current keyboardFocusManager.private javax.swing.JComponent
root
the root of the component hierarchy.
-
Constructor Summary
Constructors Constructor Description CompoundFocusListener(javax.swing.JComponent root)
Instantiates a CompoundFocusListener on the component hierarchy below the given component.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addManagerListener(java.awt.KeyboardFocusManager manager)
Adds all listeners to the given KeyboardFocusManager.private java.beans.PropertyChangeListener
getManagerListener()
Lazily creates and returns a property change listener to be registered on the KeyboardFocusManager.boolean
isFocused()
Return true if the root or any of its descendants is focused.protected void
permanentFocusOwnerChanged(java.awt.Component focusOwner)
Updates focused property depending on whether or not the given component is below the root's hierarchy.void
release()
Releases all listeners and internal references.private void
removeAllListeners()
Removes all property change listeners which are registered with this instance.private void
removeManagerListener(java.awt.KeyboardFocusManager manager)
Removes all listeners this instance has installed from the given KeyboardFocusManager.private void
setFocused(boolean focused)
-
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
-
-
-
Field Detail
-
root
private javax.swing.JComponent root
the root of the component hierarchy. PENDING JW: weak reference and auto-release listener?
-
managerListener
private java.beans.PropertyChangeListener managerListener
PropertyChangeListener registered with the current keyboardFocusManager.
-
focused
private boolean focused
-
-
Constructor Detail
-
CompoundFocusListener
public CompoundFocusListener(javax.swing.JComponent root)
Instantiates a CompoundFocusListener on the component hierarchy below the given component.- Parameters:
root
- the root of a component hierarchy- Throws:
java.lang.NullPointerException
- if the root is null
-
-
Method Detail
-
isFocused
public boolean isFocused()
Return true if the root or any of its descendants is focused. This is a read-only bound property, that is property change event is fired if focus is transfered into/out of root's hierarchy.- Returns:
- a boolean indicating whether or not any component in the container hierarchy below root is permanent focus owner.
-
release
public void release()
Releases all listeners and internal references.Note: this instance must not be used after calling this method.
-
removeAllListeners
private void removeAllListeners()
Removes all property change listeners which are registered with this instance.
-
permanentFocusOwnerChanged
protected void permanentFocusOwnerChanged(java.awt.Component focusOwner)
Updates focused property depending on whether or not the given component is below the root's hierarchy.Note: Does nothing if the component is null. This might not be entirely correct, but property change events from the focus manager come in pairs, with only one of the new/old value not-null.
- Parameters:
focusOwner
- the component with is the current focusOwner.
-
setFocused
private void setFocused(boolean focused)
-
addManagerListener
private void addManagerListener(java.awt.KeyboardFocusManager manager)
Adds all listeners to the given KeyboardFocusManager.- Parameters:
manager
- the KeyboardFocusManager to add internal listeners to.- See Also:
removeManagerListener(KeyboardFocusManager)
-
removeManagerListener
private void removeManagerListener(java.awt.KeyboardFocusManager manager)
Removes all listeners this instance has installed from the given KeyboardFocusManager.- Parameters:
manager
- the KeyboardFocusManager to remove internal listeners from.- See Also:
addManagerListener(KeyboardFocusManager)
-
getManagerListener
private java.beans.PropertyChangeListener getManagerListener()
Lazily creates and returns a property change listener to be registered on the KeyboardFocusManager.- Returns:
- a property change listener to be registered on the KeyboardFocusManager.
-
-