Java Beans
DENNIS MANCL
Lucent Technologies
Bell Laboratories
Murray Hill, NJ 07974
mancl@lucent.com
The Java programming language
- Java is a general purpose programming language
- originally defined by Sun Microsystems in 1996
- Java applications are very portable
- Java application consists of a set of '.class' files,
each containing the bytecode for a single Java class
- a Java program can be executed by a Java interpreter
- Java interpreters are included in many Web browsers,
including Netscape Navigator/Communicator and
Microsoft Internet Explorer
More about Java
- Java is a language that supports “object oriented programming”
- each of the basic program units in a Java program is a class
- each class contains some data (the attributes of the class)
- each class also contains some functions (the operations of the class)
- A Java application program consists of a bunch of cooperating objects of different classes
- some of the classes are written by application programmers
- other classes are part of the standard Java library
Some Java history
- First version of Java (based on Sun's Java Development Kit 1.0)
- contained a very simple set of "user interface"
classes (the Abstract Windowing Toolkit or AWT classes)
- each type of user interface object could be augmented with
an operation called action() - this is the
operation that would be called if a user interface event affected
the user interface object
- Current version of Java (based on Sun's JDK 1.1)
- the set of AWT classes is larger
- event processing can be delegated to "event listener" objects
- the average Java application will contain many
small classes - these classes cooperate to deal with all
kinds of user interface events
- Next version of Java (Sun's JDK 1.2)
- will contain an expanded set of user interface classes
(the Java Foundation Classes)
Creating Java components
- Some Java development packages permit:
- creation of simple Java applications by assembling
standard Java component classes
- runtime tailoring (or "configuration") of
individual Java classes
- This is somewhat similar to Visual Basic programming: tying together
ready-made components plus
editing certain properties of each component
- a Text Field component might have an initial string value
- a Checkbox component might be checked or not checked initially
Java Beans
- A Java Bean is a reusable software component
(actually, a Java class) that can be manipulated visually
in a builder tool
- Java Beans are just ordinary Java classes that follow
certain conventions - you don't need special tools
to create them
- It is useful to have some tools to help assemble
and configure a Bean-based application
- Examples of "builder tools":
- BeanBox (part of Sun's basic
Beans Development Kit (BDK) - available free from java.sun.com)
- Sun Java Workshop
- IBM VisualAge for Java
- Symantec Visual Cafe
- Borland JBuilder
Java Bean conventions
- There are two primary conventions that must be followed
in creating Java Bean classes:
- each "property" of the Java Bean class is implemented by defining
two public functions (a "get" function and
a "set" function)
- the "operations" of the Java Bean class are the other
public functions defined in the class
/* file Thermometer.java */
public class Thermometer {
/* these two functions define the "currentTemperature" property */
public int getCurrentTemperature() { return temp; }
public void setCurrentTemperature(int aTemp) { temp = aTemp; }
private int temp;
public void temperatureChanged(TempChangedEvent ev) { ... }
... other implementation details ...
}
Reflection in Java
- The "builder" programs can use special functions in
Java 1.1 to discover which operations are part of
each class
- the special Java functions are called the Java Reflection API
- they are used in many places in the builders
- Java builders can display a "property sheet" for each Java Bean
More Java Bean configuration
- The reflection interface may also be used by the
builder tool to help the "programmer" tie user interface
events to particular operations:
Java code is generated by the Builder tools
- Java applications can use the "set"
operations to initialize properties
- Builder tools can also create the code for
new objects that can be "registered" to receive events from the
appropriate user interface objects
- this means: a Java Bean doesn't have to be modified by the builder tools
- all of the tailoring code can be put into newly-created classes
- this is almost like "callbacks" in C and C++-based framework software
Creating a Java Bean
- Design an ordinary Java class: decide what properties
and public operations it will support
- Make sure that each property has a "get" and "set"
operation in the public interface of the Java Bean class
- Make sure that the class conforms to the Java 1.1 event model
- If the class is a "visual Bean", then it should
probably be a derived class of one of the AWT classes
(probably Component or Container)
- A Java Bean may also use some other helper classes
- You can bundle the ".class" files for the main Java Bean class
and all of the helper classes into a "Java
Archive" file (JAR file)
Java Beans make good interfaces
- Java Beans can be used to provide an interface to other
non-Java applications that use Microsoft's
ActiveX component technology
- there is a "bridge" between Java Beans and ActiveX
- Java Beans can be used to encapsulate the interface to
an external database
- the "get" and "set" operations in a Java Bean might
actually use the Java database interface classes (the
JDBC classes) to interact with a relational database
- Not all Java Beans are graphical - but they can still
be used to assemble applications using an
application builder tool
Alternative ways to make a Java Bean
- It isn't necessary to make changes to an existing class
to convert it to a Java Bean
- instead, you can create a "BeanInfo" class that defines
the properties and public operations
- for example, the BeanInfo class for the Temperature
class would be called TemperatureBeanInfo
- this class can define functions that permit the builder
tools to access the characteristics of the Java Bean
public class TemperatureBeanInfo extends java.beans.SimpleBeanInfo {
/* the following function will return an array of PropertyDescriptor
objects, one object per Java Bean property */
public PropertyDescriptor[] getPropertyDescriptors() {
...
}
}
Other ways to tailor a Java Bean
- The BeanInfo class that you create to tailor a Java Bean
can define many things that can be used by the
builder tools
- you can create custom property editors
- if you click on an ordinary property (an integer or String)
on a property sheet, you will get a chance to
change that value directly on the property sheet
- for more complicated properties, you might want to
create a custom window for setting up a new value
for the property
- there is a built-in property editor for the Color class
that permits the user to pick a color either by entering
RGB values or by selecting from a list of predefined color names
- you can identify an "icon" to be used by the builder tools
public Image getIcon(int iconKind) {
Image img = loadImage(“therm.gif”); return img;
}
Using Java Beans for Web programs
- Java applets are programs designed to run within a Web browser
- An applet is a Java class that is derived from the java.applet.Applet class
- Applets usually define the following functions:
- init() - this function will define what will be done
when the applet is first downloaded (it will usually
create some user interface objects and set them up to be displayed,
but it might also connect to a database or set up
some other resources)
- paint() - this function will be used to draw
fixed strings and images, which might need to be redrawn
any time the window is moved or becomes visible
- Under standard conditions, applets can
access files on the Web server, but not on the local "client" machine
Signed applets
- Java 1.1 has a new, more permissive security model:
- If the code for an applet is included in a JAR file,
- and if the JAR file has a "digital signature"
file added to it (using the JavaKey program),
- and the client machine installs a
"certificate file" (which is also generated by the JavaKey program),
- Then the applet will be permitted to read and write local files on the client machine.
- This "signed applet" mechanism uses public-key encryption
Conclusion
- Java Beans is a technology that makes it possible
to build flexible Java applications
- Applications can be built by non-programmers (using a builder tool)
- Creating new Java Bean classes requires certain skills:
- the developer needs to understand and use the Java 1.1 event model
- the developer must follow certain "naming
conventions" when creating the operation names in a Java Bean class
- extra information for the builder tools can be added in a BeanInfo class
- Java Beans is supported by many of the new Java development tools
- Java Beans can be used in tandem with many other technologies
- ActiveX
- relational databases