public class CompoundFocusListener extends AbstractBean
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);
PENDING JW: change of current instance of KeyboardFocusManager?Modifier and Type | Field and Description |
---|---|
private boolean |
focused |
private PropertyChangeListener |
managerListener
PropertyChangeListener registered with the current keyboardFocusManager.
|
private JComponent |
root
the root of the component hierarchy.
|
Constructor and Description |
---|
CompoundFocusListener(JComponent root)
Instantiates a CompoundFocusListener on the component hierarchy below the given
component.
|
Modifier and Type | Method and Description |
---|---|
private void |
addManagerListener(KeyboardFocusManager manager)
Adds all listeners to the given KeyboardFocusManager.
|
private 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(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(KeyboardFocusManager manager)
Removes all listeners this instance has installed from the given KeyboardFocusManager.
|
private void |
setFocused(boolean focused) |
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
private JComponent root
private PropertyChangeListener managerListener
private boolean focused
public CompoundFocusListener(JComponent root)
root
- the root of a component hierarchyNullPointerException
- if the root is nullpublic boolean isFocused()
public void release()
Note: this instance must not be used after calling this method.
private void removeAllListeners()
protected void permanentFocusOwnerChanged(Component focusOwner)
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.
focusOwner
- the component with is the current focusOwner.private void setFocused(boolean focused)
private void addManagerListener(KeyboardFocusManager manager)
manager
- the KeyboardFocusManager to add internal listeners to.removeManagerListener(KeyboardFocusManager)
private void removeManagerListener(KeyboardFocusManager manager)
manager
- the KeyboardFocusManager to remove internal listeners from.addManagerListener(KeyboardFocusManager)
private PropertyChangeListener getManagerListener()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses