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:
Write the SimpleBean code. Put it in a file
named SimpleBean.java, in the directory
of your choice. Here's the code:
import java.awt.Color;
import java.beans.XMLDecoder;
import javax.swing.JLabel;
import java.io.Serializable;
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
java.io.Serializable
interface. Your bean may implement either the Serializable or
the Externalizable interface.
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">
<manifest>
<section name="${beanname}.class">
<attribute name="Java-Bean" value="true"/>
</section>
</manifest>
</jar>
</target>
<target name="compile">
<javac destdir="${basedir}">
<src location="${basedir}"/>
</javac>
</target>
<target name="clean">
<delete file="${jarfile}">
<fileset dir="${basedir}" includes="*.class"/>
</delete>
</target>
</project>
It is recommended to save an XML script in the build.xml file,
because Ant recognizes this file name automatically.
Load the JAR file. Use the NetBeans IDE GUI Builder to load the jar file as follows:
The following figure represents the SimpleBean object loaded in the GUI Builder panel:

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.