Java Reference
In-Depth Information
manifest for the JAR file. To identify a class file as a bean, you simply add the fol-
lowing line to the file's manifest entry:
Java-Bean: true
To package the
MultiLineLabel
class in a JAR file, first create a manifest “stub”
file. Create a file, perhaps named
manifest.stub
, with these contents:
Name: com/davidflanagan/examples/beans/MultiLineLabel.class
Java-Bean: true
Note that the forward slashes in the manifest file shouldn't be changed to back-
ward slashes on Windows systems. The format of the JAR manifest file requires
forward slashes to separate directories, regardless of the platform. Having created
this partial manifest file, you can now create the JAR file:
% jar cfm MultiLineLabel.jar manifest.stub
com/davidflanagan/examples/beans/MultiLineLabel.class
com/davidflanagan/examples/beans/Alignment.class
Note that this is a single long command line that has been broken onto three lines.
Also, on a Windows system, you do need to replace forward slashes with back-
slashes in this command line. If this bean required auxiliary files, you would spec-
ify them at the end of the
jar
command line, along with the class files for the
bean.
Installing a Bean
The procedure for installing a bean depends on the beanbox tool you use. For the
beanbox
tool shipped with the BDK, all you need to do is copy the bean's JAR file
into the
jars
directory within the BDK directory. Once you have done this, the
bean appears on the palette of beans every time you start the application. Alterna-
tively, you can load a bean's JAR file at runtime by selecting the Load JAR option
from the File menu of
beanbox
.
A More Complex Bean
Example 14-3 shows another bean,
YesNoPanel
. This bean displays a message
(using
MultiLineLabel
) and three buttons to the user. It fires an event when the
user clicks on one of the buttons.
YesNoPanel
is intended for use within dialog
boxes, as it provides an ideal way to ask the user yes/no questions. Figure 14-1
shows the
YesNoPanel
being manipulated in Sun's
beanbox
tool.
The
YesNoPanel
bean uses a custom
AnswerEvent
type to notify
AnswerListener
objects when the user has clicked on one of its three buttons. This new event class
and listener interface are defined in the next section.
Notice that
YesNoPanel
doesn't use any classes from the
java.beans
package. One
of the surprising things about beans is that they typically don't have to use any
classes from this package. As you'll see later in this chapter, it's the auxiliary
classes that are shipped with a bean that make heavy use of that package.