A Sip From The Firehose: Friday, October 17, 1997

By: David Intersimone

Abstract: David I Roasts Some JavaBeans

Friday, October 17, 1997
Santa Cruz, CA

David I Roasts Some JavaBeans

There's no denying that the hottest drink happening in the United States and elsewhere right now is coffee. There seems to be a Starbucks Coffee, bookstore with a coffee bar, and even specialty drive-thru expresso stands on every corner. There's no denying components and component models are two of the hottest things happening in the world of programming. When the Java language first came out it did not include a component model. In the Java Development Kit (JDK) version 1.1, JavaSoft added the JavaBeans component architecture. Borland JBuilder R&D members were instrumental in the design of the JavaBeans specification. JBuilder ships with a bag full of JavaBeans. You can also use JBuilder to build and test your own JavaBeans either from scratch with source code or using our JavaBeans Express. Let's take a look at what is cooking with JavaBeans.

What is a JavaBean?

The simplest JavaBean is a Java class that contains a public default constructor:

  public class MyBean {
    public MyBean() {...}

What is a useful JavaBean?

A useful JavaBean exposes object properties, property editors and events. Since Java has no language syntax for properties or events, how do you create properties, property editors and events?

To create properties you pattern your access methods starting with the text "get" and "set" and complete the method name with the text you want for the property name. A JavaBeans tool, like JBuilder, looks for these patterns and exposes the names as properties. In the following example,

  public class LabelBean extends java.awt.Component {
    public LabelBean() { }
    private String label;
    public void setLabel(String newLabel) { label = newLabel; }
    public String getLabel() { return label; }

JBuilder will interpret this class as having a string property called "Label". If the class only had a "get" method this would be equivalent to a "read only" property. If the class only had a "set" method this would be a "write only" property.

JBuilder also looks for any methods that are part of an ActionListener

interface and exposes them as events. In the following example,

  public interface ActionListener
    extends java.util.EventListener {
    public void actionPerformed(ActionEvent e);

JBuilder will interpret the class has having an event called "actionPerformed". In Java, events can be multi-cast. This means you can add as many listeners for an event as you want.

To add custom property editors you create a class that extends JBuilder's BasicBeanInfo class.

  public class LabelBeanBeanInfo
      extends borland.jbcl.util.BasicBeanInfo {
    beanClass = LabelBean.class;
    public LabelBeanBeanInfo() {
      propertyDescriptors = new String[][] {
        {"Label", "Label description", "getLabel", 
         "setLabel", "LabelEditorClass"}

BasicBeanInfo contains a member variable called beanClass. You assign the class name of your bean to beanClass using the .class static member variable (defined in the Java Object class). In the constructor you define a two-dimensional array of strings that describe the name of the property, the description for the property (for a popup that is displayed when the mouse moves over the property in the inspector), the get and set methods, and the name of custom property editor class. JBuilder will figure out the type of the property.

Putting it all Together: TextBean

The following example implements a JavaBean with a String property called "Text" and can respond to mouse click events.

  package davidi;
  import java.awt.*;
  import java.awt.event.*;
  import borland.jbcl.util.*;
  public class TextBean extends java.awt.Component {
    String text = "TextBean";
    public TextBean() {
    public void setText(String text) {
      this.text = text;
    public String getText() { return text; }
    public void paint(Graphics g) {
      g.drawString(text, 0, getSize().height - 5);
    ActionMulticaster am = new ActionMulticaster();
    public void addActionListener(ActionListener l) {
    public void removeActionListener(ActionListener l) {
    protected void processMouseEvent(MouseEvent e) {
      if (e.getID() == MouseEvent.MOUSE_CLICKED) {
        am.dispatch(new ActionEvent(this,
          ActionEvent.ACTION_PERFORMED, text));

The TextBean can be placed into a Frame:

  package davidi;
  import java.awt.*;
  import java.awt.event.*;
  import borland.jbcl.control.*;
  import borland.jbcl.layout.*;
  public class Frame1 extends DecoratedFrame implements ActionListener{
    TextBean textBean = new TextBean();
    BorderLayout borderLayout2 = new BorderLayout();
    //Construct the frame
    public Frame1() {
      try {
      catch (Exception e) {
    //Component initialization
    private void jbInit() throws Exception{
      this.setSize(new Dimension(400, 300));
      this.setTitle("Frame Title");
      textBean.setBounds(10, 10, 100, 20);
      textBean.setText("Hello from California!");
    public void actionPerformed(ActionEvent e) {
      textBean.setText("You clicked me!!!");

When you click the mouse on the text in the running application it will change to "You clicked me!!!".

JBuilder is Chock Full of JavaBeans

There is much more work involved in building really useful JavaBeans. Fortunately, JBuilder comes pre-built with numerous reusable JavaBeans and many standard property editors. The JBuilder JavaBeans Component Library (JBCL) includes several hundred JavaBean components. A list of the Top 100 JavaBeans that most fully support development using the JBuilder visual design tools, without writing code are available on Borland Online. If the hundreds of JavaBeans included with JBuilder aren't enough, you can find a marketplace of JBuilder Tools and Components available from our list of supporting ISVs.

If you have an investment in applications that use ActiveX controls, you can use JavaSoft's JavaBeans Bridge for ActiveX. The JavaBeans bridge allows you to use JavaBeans in OLE, COM and ActiveX containers (Microsoft Word, Internet Explorer, Delphi applications, etc.). The bridge puts a ActiveX wrapper around the JavaBean.

To keep up to date on what is happening with JBuilder and JavaBeans, use the JBuilder Newsgroups and subscribe to the JBuilder ListServ.

Good Luck Roasting Your Own JavaBeans!

David Intersimone
[email protected]

Server Response from: ETNASC04