Java Reference
In-Depth Information
} else if (e.getPropertyName().equals(Action.SMALL_ICON)) {
Icon icon = (Icon)e.getNewValue();
button.setIcon(icon);
button.invalidate();
button.repaint();
}
}
}
■
Note
You can bind a
PropertyChangeListener
to a specific property by adding the listener with
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
.
This allows your listener to avoid having to check for the specific property that changed.
Listening to JComponent Events with a VetoableChangeListener
The
VetoableChangeListener
is another JavaBeans listener that Swing components use.
It works with constrained properties, whereas the
PropertyChangeListener
works with
only bound properties. A key difference between the two is that the
public void
vetoableChange(PropertyChangeEvent propertyChangeEvent)
method can throw a
PropertyVetoException
if the listener doesn't like the requested change.
public interface VetoableChangeListener extends EventListener {
public void vetoableChange(PropertyChangeEvent propertyChangeEvent)
throws PropertyVetoException;
}
■
Note
Only one Swing class,
JInternalFrame
, has constrained properties. The listener is meant primarily for
programmers to use with their own newly created components.
Listening to JComponent Events with an AncestorListener
You can use an
AncestorListener
to find out when a component moves, is made visible, or is
made invisible. It's useful if you permit your users to customize their screens by moving
components around and possibly removing components from the screens.
public interface AncestorListener extends EventListener {
public void ancestorAdded(AncestorEvent ancestorEvent);
public void ancestorMoved(AncestorEvent ancestorEvent);
public void ancestorRemoved(AncestorEvent ancestorEvent);
}