Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -

Similar presentations


Presentation on theme: "Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -"— Presentation transcript:

1 Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -

2 Mari Göransson - KaU - Datavetenskap - DAVD11 2 Properties Simple properties Bound properties Constrained properties

3 Mari Göransson - KaU - Datavetenskap - DAVD11 3 Simple Properties Named attributes that can be read or written by calling appropriate methods on the JavaBean E.g. Getter: String getTextValue() E.g. Setter: setTextValue(String) -> property is textValue

4 Mari Göransson - KaU - Datavetenskap - DAVD11 4 Bound Properties When changes to the property are of interest to other beans. The component provides a change notification service: public void addPropertyChangeListener(PropertyChangeListener l) public void removePropertyChangeListener(PropertyChangeListener l)

5 Mari Göransson - KaU - Datavetenskap - DAVD11 5 Bound Properties Mostly, the listeners are handled by the class PropertyChangeSupport. When a change occurs a PropertyChangeEvent is fired containing the old and new value.

6 Mari Göransson - KaU - Datavetenskap - DAVD11 6 Constrained Properties When a bean (listener) may want to validate a change and reject the change if it is inappropriate.

7 Mari Göransson - KaU - Datavetenskap - DAVD11 7 Event-mechanism models The basic event-delegation mechanism is a specialized version of the Observer pattern. JavaBeans introduced another version of the observer pattern; the property change listener.

8 Mari Göransson - KaU - Datavetenskap - DAVD11 8 Java Event Handling and the Observer Pattern The observer pattern is used when an object wants to be informed of a state change in another object. In java, the observer object listens for events to happen.

9 Mari Göransson - KaU - Datavetenskap - DAVD11 9 Java Event Handling and the Observer Pattern

10 Mari Göransson - KaU - Datavetenskap - DAVD11 10 Java Event Handling and the Observer Pattern

11 Mari Göransson - KaU - Datavetenskap - DAVD11 11 JavaBeans and Events

12 Mari Göransson - KaU - Datavetenskap - DAVD11 12 Property Change Listeners as Observers This is a closer representation of the Observer pattern: each observer listens for changes to an attribute in the subject and is informed when the state changes.

13 Mari Göransson - KaU - Datavetenskap - DAVD11 13 PropertyChangeListeners The registered set of listeners is managed within the PropertyChangeSupport class. When the watched property changes the support class informs all registered listeners of the old and new value.

14 Mari Göransson - KaU - Datavetenskap - DAVD11 14 Swing The Swing components have a similar event model where many of their properties are bound.

15 Mari Göransson - KaU - Datavetenskap - DAVD11 15 Bound Property Structure

16 Mari Göransson - KaU - Datavetenskap - DAVD11 16 Implementing Bound Property Support Import the java.beans package. This gives you access to the PropertyChangeSupport class.

17 Mari Göransson - KaU - Datavetenskap - DAVD11 17 Implementing Bound Property Support Instantiate a PropertyChangeSupport object: private PropertyChangeSupport changes = new PropertyChangeSupport(this); This object maintains the property change listener list and fires property change events on your behalf.

18 Mari Göransson - KaU - Datavetenskap - DAVD11 18 Implementing Bound Property Support Implement methods to maintain the property change listener list. Since PropertyChangeSupport implements these methods, you merely wrap calls to the property-change support object's methods:

19 Mari Göransson - KaU - Datavetenskap - DAVD11 19 Implementing Bound Property Support public void addPropertyChangeListener( PropertyChangeListener l) { changes.addPropertyChangeListener(l); } public void removePropertyChangeListener( PropertyChangeListener l) { changes.removePropertyChangeListener(l); }

20 Mari Göransson - KaU - Datavetenskap - DAVD11 20 Implementing Bound Property Support Modify a property's setter method to fire a property change event when the property is changed. Public void setText(String value) { String oldValue = text; text = value; changes.firePropertyChange(”text", oldValue, value); }

21 Mari Göransson - KaU - Datavetenskap - DAVD11 21 Implementing Bound Property Support The firePropertyChange method bundles its parameters into a PropertyChangeEvent object, and calls propertyChange(PropertyChangeEvent pce) on each registered listener.

22 Mari Göransson - KaU - Datavetenskap - DAVD11 22 Implementing Bound Property Listeners Implement the PropertyChangeListener interface. public class MyClass implements java.beans.PropertyChangeListener, java.io.Serializable

23 Mari Göransson - KaU - Datavetenskap - DAVD11 23 Implementing Bound Property Listeners Implement the propertyChange method in the listener. public void propertyChange(PropertyChangeEvent e) Example taken from: http://java.sun.com/docs/books/tutorial/javabeans/properties/bound.html http://java.sun.com/docs/books/tutorial/javabeans/properties/bound.html


Download ppt "Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -"

Similar presentations


Ads by Google