The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Trail: JavaBeans(TM)

Lesson: Writing a Simple Bean

In this section you will learn more about Beans and the BeanBox by

Your Bean will be named SimpleBean. Here are the steps to create it and view it in the BeanBox:

  1. Write the SimpleBean code. Put it in a file named SimpleBean.java, in the directory of your choice. Here's the code:
       
    import java.awt.*;
    import java.io.Serializable;
             
    public class SimpleBean extends Canvas
                            implements Serializable
    {
        //Constructor sets inherited properties
        public SimpleBean(){
            setSize(60,40);
            setBackground(Color.red);
        }
    }
    

    SimpleBean extends the java.awt.Canvas(in the API reference documentation) component. SimpleBean also implements the java.io.Serializable(in the API reference documentation) interface, a requirement for all Beans. SimpleBean sets the background color and component size.

  2. Make sure the CLASSPATH environment variable is set to point to all needed .class (or .jar) files. Here are some URLs that will help you to set CLASSPATH correctly:

  3. Compile the Bean:
          javac SimpleBean.java
         
    This produces the class file SimpleBean.class

  4. Create a manifest file. Use your favorite text editor to create a file, we'll call it manifest.tmp, that contains the following text:
         Name: SimpleBean.class
         Java-Bean: True
         
  5. Create the JAR file. The JAR file will contain the manifest and the SimpleBean class file:
         jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
         
    See the JAR Files(in the JavaBeans(TM) trail) trail, and the JDK JAR file documentation(outside of the tutorial) for complete information on JAR files.

  6. Load the JAR file into the ToolBox. Select the File|LoadJar... menu item. This will bring up a file browser. Navigate to the SimpleBean.jar location and select it. SimpleBean will appear at the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files in the beans/jars directory are automatically loaded into the ToolBox).

  7. Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean in the ToolBox. The cursor will change to a crosshair. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with a hatched border. This border means that SimpleBean is selected. The SimpleBean properties will appear in the Properties sheet.

You can resize SimpleBean, because it inherits from Canvas, by dragging a corner. You will see the cursor change to a right angle when over a corner. You can also reposition SimpleBean within the BeanBox by dragging on any non-corner portion of the hatched border. You will see the cursor change to crossed arrows when in position to move the Bean.

SimpleBean Makefiles

Below are two makefiles (Unix and Windows) set up to create SimpleBean.


# gnumake file CLASSFILES= SimpleBean.class JARFILE= SimpleBean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) echo "Name: SimpleBean.class" >> manifest.tmp echo "Java-Bean: True" >> manifest.tmp jar cfm $(JARFILE) manifest.tmp *.class @/bin/rm manifest.tmp # Compile the sources %.class: %.java export CLASSPATH; CLASSPATH=. ; \ javac $< # make clean clean: /bin/rm -f *.class /bin/rm -f $(JARFILE)

Here is the Windows nmake version:


# nmake file CLASSFILES= simplebean.class JARFILE= simplebean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) jar cfm $(JARFILE) <<manifest.tmp *.class Name: SimpleBean.class Java-Bean: True << .SUFFIXES: .java .class {sunw\demo\simple}.java{sunw\demo\simple}.class : set CLASSPATH=. javac $< clean: -del sunw\demo\simple\*.class -del $(JARFILE)

You can use these makefiles as templates for creating your own Bean makefiles. The example Bean makefiles, in the beans/demo directory, also show you how to use makefiles to build and maintain your Beans.

Inspecting SimpleBean Properties and Events

The Properties sheet displays the selected Bean's properties. With SimpleBean selected, the Properties sheet displays four propeties: foreground, background, font, and name. We declared no properties in SimpleBean (see the Properties section to learn how to declare properties), so these are properties inherited from Canvas. Clicking on each property brings up a property editor. The BeanBox provides default property editors for the primitive types, plus Font and Color types. You can find the sources for these property editors in beans/apis/sun/beans/editors.

Beans communicate with other Beans by sending and receiving event notifications. To see which events SimpleBean can send, choose the Edit|Events BeanBox menu item. A list of events, grouped by the Java interface in which the event method is declared, will be displayed. Under each interface group is a list of event methods. These are all inherited from Canvas.

You will learn more about properties and events in upcoming sections.

Generating Bean Introspection Reports

Introspection is the process of discovering a Bean's design-time features by one of two methods:

You can generate a Bean introspection report by choosing the the Edit|Report menu item. The report lists Bean events, properties, and methods, and their characteristics.

By default Bean reports are sent to the java interpreter's standard output, which is the window where you started the BeanBox. You can redirect the report to a file by changing the java interpreter command in beanbox/run.sh or run.bat to:

java sun.beanbox.BeanBoxFrame > beanreport.txt


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.