Java Reference
In-Depth Information
Tip The Swing components use the SwingPropertyChangeSupport class, instead of the
PropertyChangeSupport class, to manage and notify their PropertyChangeListener list. The Swing
version, SwingPropertyChangeSupport , isn't thread-safe, but it is faster and takes up less memory.
Assuming it is accessed from only the event-dispatch thread, the lack of thread safety is irrelevant.
Managing Listener Lists
If you're creating your own components and want those components to fire off events, you
need to maintain a list of listeners to be notified. If the listener list is for AWT events (found in
java.awt.event ), you can use the AWTEventMulticaster class for help with list management.
Prior to the Swing libraries, if the event wasn't a predefined AWT event type, you had to manage this
list of listeners yourself. With the help of the EventListenerList class in the javax.swing.event
package, you no longer need to manually manage the listener list and worry about thread
safety. And, if you ever need to get the list of listeners, you can ask a Component with public
EventListener[ ] getListeners(Class listenerType) , or one of the type-specific methods like
the getActionListeners() method of JButton . This allows you to remove listeners from an inter-
nally managed list, which helps with garbage collection.
AWTEventMulticaster Class
Whether you realize it or not, the AWTEventMulticaster class is used by each and every AWT
component to manage event listener lists. The class implements all the AWT event listeners
( ActionListener , AdjustmentListener , ComponentListener , ContainerListener , FocusListener ,
HierarchyBoundsListener , HierarchyListener , InputMethodListener , ItemListener , KeyListener ,
MouseListener , MouseMotionListener , MouseWheelListener , TextListener , WindowFocusListener ,
WindowListener , and WindowStateListener ). Whenever you call a component's method to add
or remove a listener, the AWTEventMulticaster is used for support.
If you want to create your own component and manage a list of listeners for one of these
AWT event/listener pairs, you can use the AWTEventMulticaster . As an example, let's look at
how to create a generic component that generates an ActionEvent object whenever a key is
pressed within the component. The component uses the public static String getKeyText
(int keyCode) method of KeyEvent to convert the key code to its appropriate text string and
passes this string back as the action command for the ActionEvent . Because the component
is meant to serve as the source for ActionListener observers, it needs a pair of add/remove
methods to handle the registration of listeners. This is where the AWTEventMulticaster comes
in, because it will manage the adding and removing of listeners from your listener list variable:
private ActionListener actionListenerList = null;
public void addActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.add(
actionListenerList, actionListener);
}
public void removeActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.remove(
actionListenerList, actionListener);
}
 
Search WWH ::




Custom Search