![]() ![]() ![]() ![]() |
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 (by using a makefile)
- 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 example Bean will be named
SimpleBean
, and you will create it in thebeans/demo/sunw/demo/simple
directory:
- Create the directory
beans/demo/sunw/demo/simple
. This will put SimpleBean in with the other BDK demo Beans.
- Write the SimpleBean code. Since you're new to Beans, we'll give it to you:
package sunw.demo.simple; 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 the java.io.Serializable interface, a requirement for all Beans. Setting the background color and component size is all thatSimpleBean
does.
- Write the makefile. Makefiles for Unix (gnumake) and Windows (nmake) are listed at the end of this section. These makefiles compile
SimpleBean
, and generate a JAR file inbeans/jars
(where the other demo jars reside). Save one of these makefiles as simple.gmk (Unix) or simple.mk (Windows), and put it inbeans/demo
.
- Run make from the beans/demos directory. For Unix, run
For Windows, rungnumake simple.gmkThis will compile SimpleBean, and create a JAR file in thenmake -f simple.mkbeans/jars
directory. The BeanBox looks in that directory for 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
beans/jars
and select simple.jar. 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 an instance of SimpleBean into the BeanBox. Click on the word SimpleBean in 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 that
SimpleBean
is selected. TheSimpleBean
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 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.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
, 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 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.txtSimpleBean Makefiles
Here are two makefiles (Unix and Windows) set up to create SimpleBean. The makefiles compile the
.java
files, and create a JAR with the resulting.class
file and a manifest.
# gnumake file CLASSFILES= \ sunw/demo/simple/SimpleBean.class JARFILE= ../jars/SimpleBean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) echo "Name: sunw/demo/simple/SimpleBean.class" >> manifest.tmp echo "Java-Bean: True" >> manifest.tmp jar cfm $(JARFILE) manifest.tmp sunw/demo/simple/*.class @/bin/rm manifest.tmp # Compile the sources %.class: %.java export CLASSPATH; CLASSPATH=. ; \ javac $< # make clean clean: /bin/rm -f sunw/demo/simple/*.class /bin/rm -f $(JARFILE)
Here is the Windows nmake version:
# nmake file CLASSFILES= \ sunw\demo\simple\simplebean.class \ JARFILE= ..\jars\simplebean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) jar cfm $(JARFILE) << manifest.tmp sunw\demo\simple\*.class Name: sunw/demo/simple/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 JavaSoft website contains documentation on JAR files.
![]() ![]() ![]() ![]() |
BeanBox Basics |