A java.awt Example
The components provided by the
java.awt package
expose properties using the techniques just described. These user
interface elements can be manipulated in the same way as any other
object that conforms to the JavaBeans architecture. So let’s
create an applet that illustrates the concepts of bound and
constrained properties.
Our applet will have a data member named
primaryLabel which is an instance of class
NumberLabel. A NumberLabel is a
subclass of java.awt.Label that has a bound and
constrained property named Value. The
Value property is of type
int, and the label always displays the contents of
this property. We also have another instance of the
NumberLabel class, named
mirrorLabel, that will bind itself to the
Value property of
primaryLabel, and will keep its display consistent
with it. There is one button to decrement the
Value property of
primaryLabel, and one to increment it; these
button instances will be named decButton and
incButton, respectively. Lastly, we will create an
instance of class Constrainer, named
cnstr, that constrains the
Value property of
primaryLabel to be between 10 and 20. A diagram of
the interactions that will take place between the objects is shown in
Figure 4.3.

Figure 4-3. Constraining a property with a VetoableChangeEvent
Let’s take a look at the code for the
NumberLabel class first:
import java.applet.*; import java.awt.*; import ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access