|
|
BeanBox Basics |
In this section you will continue learning some BeanBox fundamentals by
- Creating a minimal 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 minimal 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 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); } }
SimpleBeanextends thejava.awt.Canvascomponent.SimpleBeanalso implements the java.io.Serializable interface, a requirement for all Beans. Setting the background color and component size is all thatSimpleBeandoes.
- Make sure the
CLASSPATHenvironment variable is set to point to all needed.class(or.jar) files.
- 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
SimpleBeanclass file:jar cfm SimpleBean.jar manifest.tmp SimpleBean.classThe JavaSoft website contains documentation on JAR files.
- Load the JAR file into the ToolBox. Pull down the File|LoadJar... menu item. This will bring up a file browser. Navigate to the
SimpleBean.jarlocation and select it.SimpleBeanwill appear at the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files in thebeans/jarsdirectory are automatically loaded into the ToolBox).
- Drop a
SimpleBeaninstance into the BeanBox. Click on the wordSimpleBeanin the ToolBox. The cursor will change to crosshairs. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with hatched border. This border means thatSimpleBeanis selected. TheSimpleBeanproperties 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 repositionSimpleBeanwithin 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:
You can use these makefiles as templates for creating your own Bean makefiles. The example Bean makefiles (
# 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)
beans/demo) will 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
SimpleBeanselected, the Properties sheet displays four propeties:foreground,background,font, andname. We declared no properties in SimpleBean (you will see how later), 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 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 interface, 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:
- 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
javainterpreter'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
|
|
BeanBox Basics |