swing propChangeListener: pass info between event generator/callback Methods

background — callback/event-handling/listener is one of the more interesting or tricky part of swing development.

Callback site can use EventObject.getSource() to find the generator Object (for a semantic event), cast to a specific type, then access its specific methods or fields. This requires prior knowledge of the exact data type of the event-generator object. That leads to tight-coupling.

A more generic solution is the PropertyChangeListener.java. This and related classes are part of java.beans package, not limited to swing. PCL is a flexible and practical solution in a wide variety of contexts beyond swing.

PropertyChangeEvent.java holds (in addition to source) propertyKeyName, oldValue and newValue fields.
Any Listener class can implement the PropertyChangeListener.java interface and support a single callback method — propertyChange(PropertyChangeEvent). When the callback runs, at run-time it has access to all 4 constituents of the PropertyChangeEvent instance.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s