Start of Tutorial > Start of Trail |
Search
Feedback Form |
In this section you will learn more about Beans and the BeanBox by
- Creating a simple Bean
- Compiling and saving the Bean into a Java Archive (JAR) file
- Loading the Bean into the ToolBox
- Dropping a Bean instance into the BeanBox
- Inspecting the Bean's properties, methods, and events
- Generating an introspection report
Your Bean will be named
SimpleBean
. Here are the steps to create it and view it in the BeanBox:
- Write the
SimpleBean
code. Put it in a file namedSimpleBean.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 thejava.awt.Canvas
component.SimpleBean
also implements thejava.io.Serializable
interface, a requirement for all Beans.SimpleBean
sets the background color and component size.
- 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:
- The Managing Source and Class Files lesson gives good advice on how and when to set your CLASSPATH.
- The JDK Tool Reference Page provides complete CLASSPATH information for both Windows and Solaris platforms.
- Compile the Bean:
javac SimpleBean.javaThis produces the class fileSimpleBean.class
- 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- Create the JAR file. The JAR file will contain the manifest and the
SimpleBean
class file:jar cfm SimpleBean.jar manifest.tmp SimpleBean.classSee the JAR Files trail, and the JDK JAR file documentation for complete information on JAR files.
- 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 thebeans/jars
directory are automatically loaded into the ToolBox).
- Drop a
SimpleBean
instance into the BeanBox. Click on the wordSimpleBean
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 thatSimpleBean
is selected. TheSimpleBean
properties will appear in the Properties sheet.You can resize
SimpleBean
, because it inherits fromCanvas
, by dragging a corner. You will see the cursor change to a right angle when over a corner. You can also repositionSimpleBean
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.
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.
The Properties sheet displays the selected Bean's properties. With
SimpleBean
selected, the Properties sheet displays four propeties:foreground
,background
,font
, andname
. We declared no properties inSimpleBean
(see the Properties section to learn how to declare properties), so these are properties inherited fromCanvas
. Clicking on each property brings up a property editor. The BeanBox provides default property editors for the primitive types, plusFont
andColor
types. You can find the sources for these property editors inbeans/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.
Introspection is the process of discovering a Bean's design-time features by one of two methods:
- Low-level reflection, which uses design patterns to discover your Bean's features
- By examining an associated bean information class that explicitly describes your Bean's features.
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 inbeanbox/run.sh
orrun.bat
to:java sun.beanbox.BeanBoxFrame > beanreport.txt
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.