Title: JavaBeans
1JavaBeans
2What We Want
- Reusable components
- Components don't depend on other components
- Can configure a component visually and discover
at "design time" the properites of the component - Example clock
- we want a clock that can be easily added to any
GUI application - want to set things such as font, time, background
color
3JavaBeans
- A JavaBean is
- a reusable component
- packaged as a JAR file containing one or more
classes - exposes properties and methods that other
applications can use - has a Facade Class (like a main class) with a
default, no parameter constructor - Facade class obeys the Java convention for
get/set methods
4JavaBean Details
- A JavaBean must...
- implement java.io.Serializable
- contain a manifest that includes this value
- JavaBean true
- follow Java convention for naming get/set
methods - private String name
- public String getName() ...
- public void setName(String name) ...
54 Kinds of Properties
- simple - property has a single value that is
independent of changes to other properties - indexed - (typically an array) a property that
has many values, indexed by something - bound - a property for which other Beans need to
be notified whenever a change occurs - constrained - the value of the property must be
verified by another bean.
6Simple Property
- A title
- A boolean, read-only property
private String title public String getTitle( )
return title public String setTitle( String
title ) this.title title
private boolean full public boolean isFull( )
return full
7Indexed Property
- Indexed properties need 2 get and 2 set methods
/ course grades / private String grade /
get one grade / public String getGrade(int index
) return gradeindex / set one grade
/ public void setGrade( index index, String
value ) gradeindex value / get all
grades / public String getGrade( ) return
grades / set all grades / public void
setGrade(String newgrades) ...
8Bound Properties
- These are what make JavaBeans interesting
- Support "observers" using the PropertyChangeListen
er interface
PropertyChangeListener propertyChange( e
PropertyChangeEvent )
PropertyChangeSupport - listeners Collection -
properties String many methods
addPropertyChangeListener(this)
YourJavaBean -setProperty( value )
firePropertyChange( "property", old, new )
AnObserverClass propertyChange( ... ) ...