A Basic Bean

I’ve put together a very simple Bean, SimpleBean.

package coyote.bean;

import java.awt.*;

//
//
// SimpleBean
//
//
public class SimpleBean extends Canvas 
{

    public SimpleBean()
    {
        setBackground(Color.blue);
    }

    public Dimension getMinSize()
    {
        return new Dimension(64, 64);
    }

}

SimpleBean looks no different from any other Java class; what makes it a Bean is the way that it is packaged and accessed by other software components. You store Java Beans in a JAR—not the glass kind, but a Java archive file. A JAR uses the familiar ZIP file format to store a related collection of classes and interfaces. The components of an Internet applet—its .class files, graphics images, sounds, and so forth—can be stored as a group in a single JAR file. The client can then quickly download this single compressed file, which the Java virtual machine unpacks and uses. A manifest tells the JVM how to use each item in a JAR. You need a basic understanding of JARs if you are to correctly implement Java Beans. The rest of this section will give you sufficient information about JARs to allow you to use them with Beans. The “More about JARs” sidebar explains the non-Bean aspects of using JARs.

More About JARs

A JAR file combines several files into one, using a variation on the popular ZIP format. One of the key benefits of JARs is that they can deliver applets efficiently: a client can download a complete applet—including code, images, and other components—in a single HTTP transaction. Also, the components of a JAR file can be compressed, further shortening the download time. As Java applications grow larger, JAR files become increasingly important in speeding up applet delivery over the Internet.

To add a JARed applet to your web page, use the archive parameter of the APPLET tag.

    <applet code=MyApplet.class
archive="jars/MyApplet.jar"
width=320 height=240>
<param name=bounce value="high">
</applet>

The code parameter identifies the Applet class, just as it does for a non-JARed applet; the additional archive parameter tells the virtual machine that MyApplet.class is found in MyApplet.jar.

The first file in a JAR can be a manifest that describes the other JAR components; Java makes special use of the manifest to identify Beans. The manifest can also declare a “signature” value to identify the origin of an applet component. Without a manifest, the virtual machine cannot identify Java Beans components. See Chapter 11 for more information on signing applets using a JAR or with Microsoft’s Authenticode technology and CAB files.

A JAR file can be edited and manipulated with any ZIP-compatible archive manager, including the original PKZip from PKWare. You can create a JAR file with the jar program available from Sun Microsystems, or by using a standard ZIP-compatible utility; in the latter case, simply rename the output file from .zip to .jar. However, the manifest is stored in a compiled format generated by jar; thus you cannot include a manifest when creating a JAR with a general-purpose archive manager.

© 1997 by Scott Ladd. All rights reserved.