Given by Nancy McCracken at UC Web applications Certificate on Aug 11,14 97. Foils prepared Aug 14 1997
Outside Index
Summary of Material
How to make and test a Java Bean
|
Bean Properties |
Hooking up Bean Events |
Other issues: Serialization, BeanInfo, Customization |
Outside Index Summary of Material
8/14/97 |
Nancy McCracken |
NPAC |
How to make and test a Java Bean
|
Bean Properties |
Hooking up Bean Events |
Other issues: Serialization, BeanInfo, Customization |
To a standard hello world Java class, we add a (read/write) property by defining get and set functions in the correct pattern. |
import java.awt.* |
public class HelloBean extends java.applet.Applet |
implements java.io.Serializable |
{ String sname = "World"; |
public void setName (String newname) |
{ sname = newname; } |
public String getName ( ) |
{ return sname; } |
Enables saving as Object |
Class variable for value of property |
Get and set methods define a property called Name with a value of type String |
Additional methods make the bean have a visualization (canvas) |
public Dimension getMinimumSize() |
{ return new Dimension (250, 70); } |
public void paint(Graphics g) |
{Font f = new Font("TimesRoman", Font.BOLD, 36); |
g.setFont(f); |
g.setColor(Color.red); |
g.drawString("Hello " + sname + "!", 5, 50); |
} |
} |
Use the paint method to draw on the canvas |
New JDK1.1 method to tell Layout manager the minimum size of the canvas - also defines a (read only) property. |
After compiling the .java file, convert the .class file to a JAR file. |
Among the archive options are
|
Run the BeanBox and use the Loadjar command to open your new Bean. |
Use as many copies as you want, using the property editor to change the name. |
Giving a set method defines a write property, and a get method defines a read property, according to this pattern: public void set<PropertyName>(<PropertyType> value); public <PropertyType> get<PropertyName> ( ); |
If the property type is boolean, the get() method can be replaced or augmented by a method: public boolean is<PropertyName> ( ); |
Properties can also have arrays of values. For example, you can have a property named StockList, which had an indexed collection of Strings as its value: public String [ ] getStockList ( ); public void setStockList ( String [ ] values); |
And you can give methods to access individual values: public String getStockList (int index); public void setStockList (int index, String value); |
A property is bound if a change to its value can be reported as an event to other beans. |
One way is to inherit from the class: public class java.beans.PropertyChangeSupport implements java.io.Serializable ... public synchronized void addPropertyChangeListener ... public synchronized void removePropertyChangeListener ... public void firePropertyChange (String propertyName, Object oldValue, Object newValue); |
This class behaves as a source of events. |
A related concept is when another bean wants to have a possible veto over a change to a property. |
The set method of the property throws an exception called java.beans.PropertyVetoException. |
The object must also support (by having add and remove methods) a VetoableChangeListener. |
Then each bean that wants to be able to veto property changes implements VetoableChangeListener. |
If more than one bean wants to veto property changes, then it may be quite complicated to work out when to keep the old value or move to the new value. |
Beans must implement java.io.Serializable if they are to be saved, or used as components in other beans that are saved. |
There are no methods to provide - implementing Serializable is a promise that all member data of the class can be successfully saved and restored used the serialization mechanism. |
Static and transient members are not saved. |
Any class that would present a security risk can't be serializable, for example a FileInputStream can't be saved because it contains a file handle. |
In the BeanBox, use the Save and Load functions under the File menu. |
Consider the standard example of hooking up two buttons to start and stop the Juggler animation bean. |
First we place a button and name it start. We want to hook up its actionEvent with the method called start() in the Juggler, which starts the animation. |
The BeanBox allows us to select the actionPerformed event from the "button push" menu, select the Juggler bean to be the target, and select its start method. |
What this does is to create a class that implements an ActionListener (or inherits from the ActionListener adapter class). |
This is the automatically generated file for the Juggler: package tmp.sun.beanbox; public class __Hookup_140a49964da implements java.awt.event.ActionListener, java.io.Serializable { private sunw.demo.juggler.Juggler target; public void setTarget(sunw.demo.juggler.Juggler t) { target = t; } public void actionPerformed (java.awt.event.ActionEvent arg0) { target.startJuggling(); } } |
So far the beans we've seen have used standard patterns to expose their properties, methods, and events to a builder's tool. For more general introspection, you can proved a class specifically to describe the bean. |
This class implements the java.beans.BeanInfo interface. |
If you have a Bean class named Xyz, the its BeanInfo class is named XyzBeanInfo. |
If you don't want to supply new versions of all the methods in the BeanInfo interface, you can extend the class java.beans.SimpleBeanInfo, which provide null methods. For any method which is null, a builder's tool will use the standard reflection to find standard properties, etc. |
getAdditionalBeanInfo() getBeanDescriptor() getDefaultEventIndex() getDefaultPropertyIndex() getEventSetDescriptors() getIcon() getMethodDescriptors() getPropertyDescriptors() |
The Property Sheet box uses default java classes to read and write any property. You can provide your own methods by implementing the java.beans.PropertyEditor class. |
In addition, if you want to do more complex design time customization of the bean, possibly involving many properties, you can implement java.beans.Customizer.
|