You are on page 1of 6

Cardiff University

School of Computer Science & Informatics

Prifysgol Caerdydd
Ysgol Cyfrifiadureg a Gwybodeg

Introductory Note 615

JavaBeans

Robert Evans

14th September, 2010

c
Copyright 2010 Robert Evans.
Email: Robert.Evans@cs.cardiff.ac.uk

Abstract
This Note describes how to use the Java Beans Development Kit on Linux.

Contents
1 JavaBeans and the Bean Development Kit 1
1.1 The BDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Running the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Adding beans to the canvas . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Wiring up events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Writing beans and adding them to the BeanBox 4


2.1 Packaging a bean with makebean . . . . . . . . . . . . . . . . . . . . . 4
2.2 Bean name, multi-module beans and manifests . . . . . . . . . . . . . . 5
2.3 Using your bean in the BeanBox . . . . . . . . . . . . . . . . . . . . . . 6

1 JavaBeans and the Bean Development Kit


JavaBeans are reusable software components written in Java. These components
may be built into an application using an appropriate building environment. The Bean
Development Kit (BDK) from Sun includes a simple example of a building environment
which uses beans, called the beanbox, and some sample beans.
This note will tell you how to set up and use the BDK on the Computer Science &
Informatics Fedora Linux workstations and how to add your own beans to the BeanBox
environment.
2 Computer Science & Informatics: Introductory Note 615

1.1 The BDK


The BDK needs to create some directories and copy some files to your filespace
before you can use it. So, the first time you want to use the BDK, give the following
command:

% bdk-user-setup

This will create a directory called BDK1.1 and sub-directories BDK1.1/jars and
BDK1.1/beanbox together with some links to files in the Bean Development Kit in-
stallation directory.

1.2 Running the BeanBox


Change directory to BDK1.1/beanbox and type the command ./run.sh:

% cd BDK1.1/beanbox
% ./run.sh &

The BeanBox starts and loads beans from the JAR (Java Archive) files in the BDK1.1/jars
directory. It then displays four windows on the screen:

the ToolBox which lists all beans the BeanBox has found,

the BeanBox which is a canvas where you will drop beans and connect them
together,

the Properties sheet which is used to set the properties of the current bean,

the MethodTracer simple debugging facility.

1.3 Adding beans to the canvas


To add beans to the BeanBox canvas, click on the beans name or icon in the ToolBox
window, then click on the location in the BeanBox canvas where you want the new
Information for Users: Introductory Note 615 3

bean to appear. The bean that appears on the BeanBox canvas is the current selected
bean (shown by a hatched border around the bean) and its properties appear in the
Properties sheet.
The properties in the Properties sheet can be edited by typing new values or by spe-
cial editors (eg for colours). For example, in the diagram below, we have added two
ExplicitButton beans and a Juggler bean to the canvas. Click on the left hand bean
labelled Press. This bean becomes the current selected bean.

In the Properties sheet,


change the name in the label
from Press to Stop. The
name on the selected button
changes to Stop when you
press the Return key.

1.4 Wiring up events

We can connect events from


a selected bean to another
bean. Let us wire up the
Stop button to stop the juggler.
Select the Stop button and
choose EditEventsbutton
pushactionPerformed.
4 Computer Science & Informatics: Introductory Note 615

Now as you move the mouse,


a red line attached to the
Stop button moves with it.
Move the mouse to the Jug-
gler bean and click the mouse
button. A new EventTargetDi-
alog window which lists avail-
able events appears.

Scroll down and choose stopJuggling. Click on OK. The actionPerformed event
from the Stop button will now cause the stopJuggling method of the Juggler bean to
be called. In order to cause this to happen, the BeanBox generates adapter code,
compiles it and loads it automatically.
So clicking on the Stop button now stops the Juggler.
You can similarly set up a Start button to restart the Juggler. Choose the other Press
button on the canvas, rename it Start, choose EditEventsbutton pushactionPerformed
and this time select startJuggling from the EventTargetDialog window.

1.5 Documentation
For more information refer to the Web-based documentation at URL
http://www.cs.cf.ac.uk/applications/java/BDK1.1/.

2 Writing beans and adding them to the BeanBox


You may write your own beans which the BeanBox can use. A bean is packaged into
a JAR archive file which contains

a file, called the manifest, which specifies which class files in the archive are
beans,
all the class files associated with the component (the bean),
any data files (eg image files) needed by the bean.

Subsequent sections tell you how to create the JAR file and how to load the JAR file
into the BeanBox so that it can use your bean.

2.1 Packaging a bean with makebean


As an example, we will add the ImageViewer bean given in the book Core Java, Vol-
ume II by Horstmann and Cornell, to the BeanBox.
The source files used to build the bean must be placed in a separate directory. Use

% mkdir directory-name

to create the directory where directory-name is the name of the directory, e.g. ImageViewer,
then copy or create the source files there.
So, for the ImageViewer bean we have:
Information for Users: Introductory Note 615 5

% ls -l ImageViewer
total 68
-rw-r--r-- 1 robert 45724 Jun 1 15:39 Cay.gif
-rw-r--r-- 1 robert 1105 Jun 1 15:39 ImageViewerBean.java
-rw-r--r-- 1 robert 20132 Jun 1 15:39 Tower.gif

File ImageViewerBean.java is the Java source file and the .gif files are data files
used by the bean to draw images.
To turn these files into a JavaBean, change to the ImageViewer directory and type
makebean followed by the names of the Java and data files which make up the bean:

% makebean ImageViewerBean.java *.gif

You will see output like:

jar cfm ImageViewerBean.jar ImageViewerBean.mf *.class Cay.gif


Tower.gif

The bean is given the same name as the first Java file on the makebean command
line. (In this case, there is only one Java file and the bean is called ImageViewerBean).
The makebean command has made the ImageViewerBean.jar JavaBean JAR
archive and three other files:

% ls -l
total 137
-rw-r--r-- 1 robert 45724 Jun 1 15:39 Cay.gif
-rw-r--r-- 1 robert 1597 Jun 2 10:53 ImageViewerBean.class
-rw-r--r-- 1 robert 525 Jun 2 10:53 ImageViewerBean.gmk
-rw-r--r-- 1 robert 67275 Jun 2 10:53 ImageViewerBean.jar
-rw-r--r-- 1 robert 1105 Jun 1 15:39 ImageViewerBean.java
-rw-r--r-- 1 robert 48 Jun 2 10:53 ImageViewerBean.mf
-rw-r--r-- 1 robert 20132 Jun 1 15:39 Tower.gif

File ImageViewerBean.mf is the manifest which has been created automatically by


makebean. It specifies a single class corresponding to the first named Java file. It
looks like:

Name: ImageViewerBean.class
Java-Bean: True

The manifest file tells the BDK to display the bean ImageViewerBean in its ToolBox
when the JAR files is loaded.
ImageViewerBean.gmk is a makefile which makebean supplies to GNU make in
order to compile the Java source and build the JAR file. Makebean runs GNU make
for you, you do not have to do it yourself. ImageViewerBean.class is the class file
which has been compiled from ImageViewerBean.java. There is a copy of it in the
JAR file.

2.2 Bean name, multi-module beans and manifests


The above example is simple because the bean is made up of only one Java source
file. If you have a bean which needs more than one Java source file, you must name
all of the Java files in the makebean command. The name of the first Java file
listed is taken to be the name of the bean. E.g.
6 Computer Science & Informatics: Introductory Note 615

% makebean MyBean.java Utility.java Picture.gif

would create a JavaBean called MyBean in a file MyBean.jar. As in the case of Im-
ageViewerBean, makebean will create a manifest file specifying that the class com-
piled from the first named Java file is a bean.

Name: MyBean.class
Java-Bean: True

If more than one class file in the archive is to be a bean, you must create the manifest
file yourself before running makebean.

2.3 Using your bean in the BeanBox


The simplest way to make your bean available to the BeanBox is to move its JAR file
into the BDK jars directory, e.g.:

% mv ImageViewerBean.jar /BDK1.1/jars

The new bean will appear in the ToolBox when you next start the BeanBox (with
./run.sh).
Alternatively, you can load any JavaBean JAR file into the BeanBox by choosing
FileLoadJar... from the BeanBox menu and browsing for your JAR file.

You might also like