Java beans tutorial - contributed by Pradip Patil
Java Beans
Java beans is the software component used for the reusability of the code. Beans
can perform simple as well as complex functions. Beans are visible to the user
also .
Following terms are required to understand the exact meaning of it.
1) Builder tool: Enable visual construction of the application like visual
basic etc.
2) Software component : reusability of component like Button multiple states
etc.
Features of beans are as follows .
- Introspection permitting a builder tool to analyze how a bean works.
- Customization permitting user to alter the appearance and behavior of a bean.
- Events permitting beans to fire events, and notifying builder tools about
both the events they can fire and the events they can handle.
- Properties permitting beans to be manipulated programmatically.
- Beans can be manipulated by text tools through programmatic interfaces. All
key APIs, including support for events, properties, and persistence, have been
designed to be easily read and understood by human programmers as well as by
builder tools.
Writing simple bean:
In the simplest way you need to add a pair of methods to an existing class
definition in order to make a Bean. This can be done either by modifying the
source for an existing class, or by extending the behavior of an existing class
by subclassing it.
import java.awt.Color;
import java.beans.XMLDecoder;
import javax.swing.JLabel;
import java.io.Serializable;
public class DemoBean extends JLabel
implements Serializable
{
public DemoBean()
{
setText( "Hello world!" );
setOpaque( true );
setBackground( Color.RED );
setForeground( Color.YELLOW );
setVerticalAlignment( CENTER );
setHorizontalAlignment( CENTER );
}
}
|