Full HTML for

Basic foilset JavaBeans Examples

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
  • Create a Java class that has Bean properties or events
  • Compile it and make a JAR file
  • Load it into the BeanBox
Bean Properties
Hooking up Bean Events
Other issues: Serialization, BeanInfo, Customization

Table of Contents for full HTML of JavaBeans Examples

Denote Foils where Image Critical
Denote Foils where HTML is sufficient

1 Java Bean Stroll
2 Java Bean Examples
3 Our first example: HelloBean!
4 The rest of the HelloBean example
5 Make it a JAR file
6 Test with the BeanBox
7 Using the Bean
8 Method design patterns for Properties
9 Indexed Properties
10 Bound Properties
11 Constrained Properties
12 Object Serialization
13 Hooking up Beans with events
14 Example Adapter File
15 BeanInfo
16 BeanInfo Interface Methods
17 Property Editors and Customizers

Outside Index Summary of Material



HTML version of Basic Foils prepared Aug 14 1997

Foil 1 Java Bean Stroll

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
8/14/97
Nancy McCracken
NPAC

HTML version of Basic Foils prepared Aug 14 1997

Foil 2 Java Bean Examples

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
How to make and test a Java Bean
  • Create a Java class that has Bean properties or events
  • Compile it and make a JAR file
  • Load it into the BeanBox
Bean Properties
Hooking up Bean Events
Other issues: Serialization, BeanInfo, Customization

HTML version of Basic Foils prepared Aug 14 1997

Foil 3 Our first example: HelloBean!

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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

HTML version of Basic Foils prepared Aug 14 1997

Foil 4 The rest of the HelloBean example

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.

HTML version of Basic Foils prepared Aug 14 1997

Foil 5 Make it a JAR file

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
After compiling the .java file, convert the .class file to a JAR file.
Among the archive options are
  • c create a new archive
  • f make it a file
  • m use an explicitly given manifest file jar -cfm HelloBean.jar HelloBean.mf HelloBean.class

HTML version of Basic Foils prepared Aug 14 1997

Foil 6 Test with the BeanBox

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
Run the BeanBox and use the Loadjar command to open your new Bean.

HTML version of Basic Foils prepared Aug 14 1997

Foil 7 Using the Bean

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
Use as many copies as you want, using the property editor to change the name.

HTML version of Basic Foils prepared Aug 14 1997

Foil 8 Method design patterns for Properties

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
Giving a set method defines a write property, and a get method defines a read property, according to this pattern: public void set&#060PropertyName&#062(&#060PropertyType&#062 value); public &#060PropertyType&#062 get&#060PropertyName&#062 ( );
If the property type is boolean, the get() method can be replaced or augmented by a method: public boolean is&#060PropertyName&#062 ( );

HTML version of Basic Foils prepared Aug 14 1997

Foil 9 Indexed Properties

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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);

HTML version of Basic Foils prepared Aug 14 1997

Foil 10 Bound Properties

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.

HTML version of Basic Foils prepared Aug 14 1997

Foil 11 Constrained Properties

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.

HTML version of Basic Foils prepared Aug 14 1997

Foil 12 Object Serialization

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.

HTML version of Basic Foils prepared Aug 14 1997

Foil 13 Hooking up Beans with events

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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).

HTML version of Basic Foils prepared Aug 14 1997

Foil 14 Example Adapter File

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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(); } }

HTML version of Basic Foils prepared Aug 14 1997

Foil 15 BeanInfo

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.

HTML version of Basic Foils prepared Aug 14 1997

Foil 16 BeanInfo Interface Methods

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
getAdditionalBeanInfo() getBeanDescriptor() getDefaultEventIndex() getDefaultPropertyIndex() getEventSetDescriptors() getIcon() getMethodDescriptors() getPropertyDescriptors()

HTML version of Basic Foils prepared Aug 14 1997

Foil 17 Property Editors and Customizers

From JavaBeans Examples UC Web applications Certificate -- Aug 11,14 97. *
Full HTML Index
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.
  • You may present a panel for the user to change many properties or run other methods of the bean class.

© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Thu Jan 8 1998