Applets and Beans

Date: 4/1/2002

Creating a Bean

A JavaBean is nothing more than a class file that follows some basic rules of design. As a rule, beans are component classes, rather than applications or applets.

The basic rules of design are:

  1. All properties are declared as private, class-level variables.
  2. To make a property readable, define an accessor (getter) method that returns the value of the property.
    1. declare the method public
    2. declare the return type to be the same as the property variable
    3. begin the method name with the word 'get'
    4. finish the method name using the name of the variable, capitalizing the first letter
    5. the method takes no argument (exception to this rule)
  3. To make a property writable, define a mutator (setter) method that accepts a new value for the property.
    1. declare the method public
    2. declare the return type to be void
    3. begin the method name with the word 'set'
    4. finish the method name using the name of the variable, capitalizing the first letter
    5. the method takes one argument of the same type as the property variable (exception to this rule)

 The exception to rules 2.5 and 3.5 is if the property is indexed. A property would be indexed if the property were an array or other type of list. In this case, the accessor and mutator methods would take an additional, numeric argument for the index to the value in the list.

Additionally, properties may be bound or constrained.

A bound property is simply one that sends out a notification when it has changed. The bean maintains a list of other objects that are listening for the change notification. The other objects are added as PropertyChangeListeners. This works in a similar manner as the AWT listeners we've already used. For example, the ActionListener:

  1. An application may implement ActionListener. Because it does, it is required to have the actionPerformed() method.
  2. A button on the application may register the application as an ActionListener: btnExit.addActionListener(this).
  3. When the button is clicked, it goes through it's list of ActionListeners and calls their actionPerformed() method.

A constrained property is one that has an acceptable range of values. When the mutator method of the property is called, the property has the right to veto the change if the new value does not meet the defined contraints.

Running Applets

Refer to the CIS133 Lecture.

An additional attribute to the <applet> tag is archive. Often, an Applet may require additional supporting files. The value of the archive may be a comma-separated list of class files needed by the Applet. Another option is to place all the support files into a JAR file.

Using the JAR Utility

The JAR utility works in the same manner as a ZIP utility. It combines and optionally compresses files into a single file with a "jar' extension.

The simplest ways to use the JAR utility are:

Extracting:

jar xfv jar-file-name

This extracts all files from the named JAR file. Make sure you are in the folder containing the JAR file.
The "x" means you are extracting.
The "f" means you are naming the JAR file.
The "v" means you want verbose output as the utility is processing.

Creating:

jar cfv jar-file-name files

This creates the named jar file from the list of files you pass.
The "c" means you are creating.
Files are a space-separated list of files and/or folders.