When writing programs via a text editor,
a JavaBean is no different than any other Java component.
However, if you write code using a Bean enabled Java IDE,
there is a big difference.
- The Bean can be placed on the form by selecting it from
a pallette and just clicking on the form.
- The developer can view and edit a Bean's properties
via the provided property editor.
- The Size and Position properties can be modified via the mouse
(drag and drop).
What makes a component a JavaBean? Simple.
- The component must be placed in a JAR file.
- The JAR file's manifest must identify the component as a bean.
- The JAR file must be in the ClassPath.
How to Create a JavaBean
This code will compile the source and
place the .class file in the specified directory.
- Write code similar to any other component.
Hint: It should display something, though this is not required.
- Compile the code.
- Create a manifest (a text file) that identifies the Beans.
- Place all the related .class files and
the manifest in the same directory.
- Create a JAR file which includes the contents
of the specified directory.
- In the DOS Environment, modify ClassPath to include
the location of the JAR file (or copy the JAR file to some directory
in the current path).
java -d SomeDirectory source.java
As stated previously, the development environment handles
the bean functions.
The IDE checks the manifest of the available JAR files
in order to display the beans on the palette.
Once a bean is placed on the form,
the IDE searches the .class file for methods of the form
public Type getPropName()
public void setPropName(Type variable)
It then places propName in the property list,
and expects the properties to be of the specified type.
(Notice the that the "p" is capitalized in the method names,
and lower case in the property list.)
If a property is of type Boolean (True/False), then
the methods should be IsPropName and SetPropName.
Comparison with Delphi
This is similar to Delphi's published properties.
The main differences are
- The Delphi property methods can be named anything you want.
The get and set prefixes are normally used by convention,
but they are not required.
- Delphi allows some properties to not be displayed at design time.
- In Delphi, only the property name is used in the code -
the associated method names are private and never explicily called.
Via the IDE, each bean property can be associated with
the property of another bean.
Thus, you can associate the position of a slide bar
with the value in a text field without writing
any additional code.
(Well, the bean actually needs additional code to handle this,
but that is added when the bean is designed.
When the bean is used, no additional code is added.)
While pretty cool, this is really a minor function of beans.
- JAR - Java Archive
- Conceptually similar to a zip file.
It can contain any number of files,
including .class files (compiled java files), images (jpeg),
text files, and so forth.
- A text file used to describe the contents of a JAR file.
It specifically identifies which .class files are beans.
It may specify which .class file contains a main subroutine.
- IDE - Integrated Design Environment
- Basically a source editor
with additional capabilities, such as an integrated compiler
which also shows you where the errors are.
Author: Robert Clemenzi -