JavaBean Properties
Home ] Up ] What are JavaBeans? ] The Bean Development Kit ] The BeanBox ] [ JavaBean Properties ] Simple and Indexed Properties ] Bound Properties ] Constrained Properties ] Bean Deployment ] Introspection ] Property Editors ] Customizers ] Bean Persistence ] New JavaBean Features ]

 

 

A Simple JavaBean

Here's a simple JavaBean -- a bar chart -- displayed within an applet:

Did you forget to enable Java support in your browser? You'll notice that the bar chart changes as you click on each button.

If you click on the "+10%" button, the red area will move up the bar chart, while a click on the "-10%" button moves it down.

Here is the source for the bar chart bean:

package javaBeans;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

/**
 *  A BarChart JavaBean class.
 *
 *  @author Bryan J. Higgs, 12 March, 2000
 */
public class BarChart extends Canvas
{
    /**
     *  Constructs a BarChart instance
     */
    public BarChart()
    {
        setSize(m_barWidth, m_barHeight);
    }
    
    /**
     *  Sets the percent for bar display.
     */
    public void setPercent(int percent)
    {
        if (percent <= 100 && percent >= 0)
        {
            m_percent = percent;
            repaint();
        }
    }
    
    /**
     *  Gets the percent for bar display.
     */
    public int getPercent()
    {
        return m_percent;
    }
    
    /**
     *  Paints the BarChart using the specified Graphics.
     */
    public void paint(Graphics g)
    {
        g.setColor(m_background);
        g.fillRect(0, 0, m_barWidth, m_barHeight);
        g.setColor(Color.black);
        for (int i = 0; i < m_borderWidth; i++)
        {
            g.drawRect(i, i, m_barWidth-(2*i), m_barHeight-(2*i));
        }
        
        g.setColor(m_foreground);
        g.fillRect(m_borderWidth, 
                   (m_barHeight*(100 - m_percent)/100) - (m_borderWidth - 1),
                   m_barWidth-(2*m_borderWidth)+1, 
                   m_barHeight*m_percent/100 );
    }
    
    //// Private data ////
    
    private final int   m_borderWidth = 2;
    
    private int         m_percent = 0;
    private Color       m_background = Color.blue;
    private Color       m_foreground = Color.red;
    private int         m_barWidth = 25;
    private int         m_barHeight = 150;
}

and here is the source for the applet that displays it:

package javaBeans;

import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *  BarchartApplet -- a class to display a BarChart Bean.
 *
 *  @author Bryan J. Higgs, 12 March, 2000
 */
public class BarchartApplet extends Applet
{
    /**
     * Constructs a BarchartApplet
     */
    public BarchartApplet()
    {
        setLayout( new FlowLayout( FlowLayout.CENTER ) );
        // Add the up button, and handle its action event
        add(m_buttonUp);
        m_buttonUp.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ev)
                {
                    m_barChart.setPercent(m_barChart.getPercent() + 10);
                }
            }
        );
        // Add the BarChart
        add(m_barChart);
        // Add the down button, and handle its action event
        add(m_buttonDown);
        m_buttonDown.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ev)
                {
                    m_barChart.setPercent(m_barChart.getPercent() - 10);
                }
            }
        );
    }
    
    //// Private data ////
    private BarChart    m_barChart   = new BarChart();
    private Button      m_buttonUp   = new Button("+10%");
    private Button      m_buttonDown = new Button("-10%");
}

So what makes the BarChart class a JavaBean?

Remember that a JavaBean exports properties, events, and methods. It exports properties through public set and get methods. In this case, there is a single exported property, percent, which is exported via the methods setPercent() and getPercent().

Another requirement for a JavaBean is that it implement the java.io.Serializable . While the above BarChart class does not implement Serializable explicitly, it does implement it implicitly, by virtue of extending (via Canvas) class java.awt.Component, which implements Serializable. All AWT classes extending from Component are JavaBeans.

Properties

Properties are attributes of a bean that are changeable at design time (in the IDE), as well as at run time. A JavaBean-enabled IDE discovers a bean's properties through a process called introspection. (We'll talk more about introspection later.) It will:

  • Determine a bean's properties
  • Determine each property's' read/write attributes
  • Determine each property's type
  • Locate an appropriate property editor for each property type
  • Display the bean's properties (usually in a properties window, common called a property sheet)
  • Allow the user to change the bean's properties (at design time)

Simple Property Naming Patterns

Naming patterns are used by the introspection process to determine the properties of a bean. The naming pattern for properties is very simple. Any pair of methods with the signatures:

public Type getPropertyName()
public void setPropertyName(Type t)

exports a read/write property PropertyName, of type Type. For example, in the above BarChart class, there are methods:

public int getPercent()
public void setPercent(int pct)

which export a read/write property percent, of type int, for that bean.

If you have a get method without a corresponding set method, then the property is read-only.

Property Name Capitalization

Note that a pair of get/set methods:

public int getPercent()
public void setPercent(int pct)

define a property percent (with a lowercase first character)

Get Methods for boolean Type Properties

To make code read more naturally, a boolean property's get method can have the following signature:

public boolean isPropertyName()

while the set method signature remains the same. For example:

public boolean isRunning()
public void setRunning(boolean b)

would export a read/write property running, of type boolean.

 
The page was last updated February 19, 2008