Lesson 5 | Properties and Bean state |
Objective | Learn about properties and Bean state |
JavaBean Types and Properties
Properties commonly cause changes in the appearance of a Bean upon being modified.
As an example, consider a graphical user interface Bean that has its background color represented by a property. If this color property is changed, it is necessary for the Bean to repaint itself in order for the change to affect the Bean's appearance.
This is necessary because the Bean must visually reflect its state at all times. If the Bean didn't repaint itself, it would be displaying the previous background color while its background color property would have the new value. This inconsistency is not allowed in JavaBeans, which is why property modification often results in the appearance of a Bean changing.
JavaBeans Events
Events are messages sent from one object to another, notifying the recipient that something interesting has happened.
The component sending the event is said to fire the event; the recipient is called a listener, and is said to handle the event. Many objects can listen for a particular event; thus, components must be able to fire an event to an arbitrary number of objects.
Java allows you to define events that can have at most one listener, but outside of this special case, you must assume that there can be many listeners. Likewise, an object is free to listen for as many different events as it desires. The process of firing and handling events is a common feature of windowing systems and many programmers learned how
to write software using this mechanism when they began writing code for Microsoft Windows or the X Windows system.
Now the techniques of object-oriented programming are showing us that the event model can be used for applications other than windowing. Firing and handling events is one of two ways that objects communicate with each other. The other is by invoking methods on each other. We will see shortly that these two mechanisms can be one and the same.
In the next lesson, accessor methods will be used to access properties.
Developing Java Beans