Trail: JavaBeans(TM)
Lesson: Writing a Simple Bean
In this section you will learn more about beans by performing the following actions:

Your bean will be named SimpleBean. Here are the steps to create it:

  1. Write the SimpleBean code. Put it in a file named, in the directory of your choice. Here's the code:
    import java.awt.Color;
    import java.beans.XMLDecoder;
    import javax.swing.JLabel;
    public class SimpleBean extends JLabel 
                            implements Serializable {
        public SimpleBean() {
            setText( "Hello world!" );
            setOpaque( true );
            setBackground( Color.RED );
            setForeground( Color.YELLOW );
            setVerticalAlignment( CENTER );
            setHorizontalAlignment( CENTER );

    SimpleBean extends the javax.swing.JLabel graphic component and inherits its properties, which makes the SimpleBean a visual component. SimpleBean also implements the interface. Your bean may implement either the Serializable or the Externalizable interface.

  2. Create a manifest, the JAR file, and the class file SimpleBean.class. Use the Apache Ant tool to create these files. Apache Ant is a Java-based build tool that enables you to generate XML-based configurations files as follows:
     <?xml version="1.0" encoding="ISO-8859-1"?>
     <project default="build">
      <dirname property="basedir" file="${ant.file}"/>
      <property name="beanname" value="SimpleBean"/>
      <property name="jarfile" value="${basedir}/${beanname}.jar"/>
      <target name="build" depends="compile">
          <jar destfile="${jarfile}" basedir="${basedir}" includes="*.class">
                  <section name="${beanname}.class">
                     <attribute name="Java-Bean" value="true"/>
      <target name="compile">
          <javac destdir="${basedir}">
              <src location="${basedir}"/>
      <target name="clean">
          <delete file="${jarfile}">
              <fileset dir="${basedir}" includes="*.class"/>

    It is recommended to save an XML script in the build.xml file, because Ant recognizes this file name automatically.

  3. Load the JAR file. Use the NetBeans IDE GUI Builder to load the jar file as follows:
    1. Start NetBeans.
    2. From the File menu select "New Project" to create a new application for your bean. You can use "Open Project" to add your bean to an existing application.
    3. Create a new application using the New Project Wizard.
    4. Select a newly created project in the List of Projects, expand the Source Packages node, and select the Default Package element.
    5. Click the right mouse button and select New|JFrameForm from the pop-up menu.
    6. Select the newly created Form node in the Project Tree. A blank form opens in the GUI Builder view of an Editor tab.
    7. Open the Palette Manager for Swing/AWT components by selecting Palette Manager in the Tools menu.
    8. In the Palette Manager window select the beans components in the Palette tree and press the "Add from JAR" button.
    9. Specify a location for your SimpleBean JAR file and follow the Add from JAR Wizard instructions.
    10. Select the Palette and Properties options from the Windows menu.
    11. Expand the beans group in the Palette window. The SimpleBean object appears. Drag the SimpleBean object to the GUI Builder panel.

    The following figure represents the SimpleBean object loaded in the GUI Builder panel:

    This figure represents the SimpleBean object loaded in the GUI Builder panel,

  4. Inspect Properties and Events. The SimpleBean properties will appear in the Properties window. For example, you can change a background property by selecting another color. To preview your form, use the Preview Design button of the GUI Builder toolbar. To inspect events associated with the SimpleBean object, switch to the Events tab of the Properties window. You will learn more about bean properties and events in the lessons that follow.

Previous page: Previous Lesson
Next page: Properties