Sending Change Events in Swing

Swing actually uses two different change event classes. The first is the standard java.beans.PropertyChangeEvent class. This class passes a reference to the object, sending the change notification, as well as the property name, its old value, and its new value. The second, javax. swing.event.ChangeEvent, is a lighter version that only passes a reference to the sending object—in other words, the name of the property that changed, as well as the old and new values, are omitted.

Note

Since the ChangeEvent class is not part of the JavaBeans specifications, properties that use this event are not “bound” according to the JavaBeans standard. In order to prevent confusion, properties that use a ChangeEvent to notify listeners of property changes have not been marked as bound in our property tables.

Because the ChangeEvent only includes a reference to the event originator, which never changes, you can always define a single, static ChangeEvent and reuse it over and over when firing events from your component.

The ChangeEvent Class

The ChangeEvent is a stripped-down version of the java.beans.PropertyChangeEvent class. This class has no methods or properties, only a constructor:

Constructor

public ChangeEvent(Object source)

The constructor for the ChangeEvent class. It takes only a single object, which represents the entity sending the event.

The ChangeEvent class extends the java.util.EventObject class. This object contains the getSource() method, which you can ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.