A
Bean Example
This chapter concludes with
an example that illustrates various aspects of Bean programming, including
introspection and using a BeanInfo
class. It also makes use of the Introspector,
PropertyDescriptor, and EventSetDescriptor classes. The
example uses three classes. The first
is a Bean called Colors, shown here:
// A simple Bean. import java.awt.*; import
java.awt.event.*;
import java.io.Serializable;
public class Colors extends Canvas implements
Serializable { transient private Color color; // not persistent
private boolean rectangular; // is persistent
public Colors() {
addMouseListener(new MouseAdapter() { public
void mousePressed(MouseEvent me) {
change();
}
});
rectangular = false; setSize(200, 100);
change();
}
public boolean getRectangular() { return rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() { color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random()); int g =
(int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r,
g, b);
}
public void paint(Graphics g) { Dimension d =
getSize();
int h = d.height; int w = d.width;
g.setColor(color); if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else {
g.fillOval(0, 0, w-1, h-1);
}
}
}
The Colors Bean displays a colored object within a frame. The color of
the component is determined by the private Color
variable color, and its shape is
determined by the private boolean variable rectangular. The constructor defines an
anonymous inner class that extends
MouseAdapter and overrides its
mousePressed( ) method. The change(
) method is invoked in response
to mouse presses. It selects a random color and then repaints the component.
The getRectangular( ) and setRectangular(
) methods provide access to the one property public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
change();
}
});
rectangular = false; setSize(200, 100);
change();
}
public boolean getRectangular() { return
rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() { color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random()); int g =
(int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r,
g, b);
}
public void paint(Graphics g) { Dimension d =
getSize();
int h = d.height; int w = d.width;
g.setColor(color); if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else {
g.fillOval(0, 0, w-1, h-1);
}
}
}
The Colors Bean displays a colored object within a frame. The color of
the component is determined by the private Color
variable color, and its shape is
determined by the private boolean variable rectangular. The constructor defines
an anonymous inner class that extends
MouseAdapter and overrides its
mousePressed( ) method. The change(
) method is invoked in response
to mouse presses. It selects a random color and then repaints the component.
The getRectangular( ) and setRectangular(
) methods provide access to the one property
public Colors() {
addMouseListener(new MouseAdapter() { public
void mousePressed(MouseEvent me) {
change();
}
});
rectangular = false; setSize(200, 100);
change();
}
public boolean getRectangular() { return
rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() { color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random()); int g =
(int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r,
g, b);
}
public void paint(Graphics g) { Dimension d =
getSize();
int h = d.height; int w = d.width;
g.setColor(color); if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else {
g.fillOval(0, 0, w-1, h-1);
}
}
}
The Colors Bean displays a colored object within a frame. The color of
the component is determined by the private Color
variable color, and its shape is
determined by the private boolean variable rectangular. The constructor defines
an anonymous inner class that extends
MouseAdapter and overrides its
mousePressed( ) method. The change(
) method is invoked in response
to mouse presses. It selects a random color and then repaints the component.
The getRectangular( ) and setRectangular(
) methods provide access to the one property of this Bean. The change( ) method calls randomColor( ) to choose a color and
then calls repaint( ) to make the
change visible. Notice that the paint( )
method uses the rectangular and color variables to determine how to
present the Bean.
The next class is ColorsBeanInfo. It is a subclass of SimpleBeanInfo that provides explicit
information about Colors. It
overrides getPropertyDescriptors( )
in order to designate which properties are presented to a Bean user. In this
case, the only property exposed is rectangular.
The method creates and returns a
PropertyDescriptor object for the
rectangular property. The PropertyDescriptor
constructor that is used is shown here:
PropertyDescriptor(String property, Class<?> beanCls) throws IntrospectionException
Here, the first argument is
the name of the property, and the second argument is the class of the Bean.
// A Bean information class.
import java.beans.*;
public class ColorsBeanInfo extends
SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor rectangular = new
PropertyDescriptor("rectangular", Colors.class);
PropertyDescriptor pd[] = {rectangular}; return
pd;
}
catch(Exception e) {
System.out.println("Exception caught. " + e);
}
return null;
}
}
The final class is called IntrospectorDemo. It uses introspection
to display the properties and events that are available within the Colors Bean.
// Show properties and events.
import java.awt.*;
import java.beans.*;
public class IntrospectorDemo {
public static void main(String args[]) { try {
Class<?> c =
Class.forName("Colors");
BeanInfo beanInfo =
Introspector.getBeanInfo(c);
System.out.println("Properties:");
PropertyDescriptor propertyDescriptor[] =
beanInfo.getPropertyDescriptors();
for(int i = 0; i <
propertyDescriptor.length; i++) { System.out.println("\t" +
propertyDescriptor[i].getName());
}
System.out.println("Events:");
EventSetDescriptor eventSetDescriptor[] =
beanInfo.getEventSetDescriptors();
for(int i = 0; i <
eventSetDescriptor.length; i++) { System.out.println("\t" +
eventSetDescriptor[i].getName());
}
}
catch(Exception e) {
System.out.println("Exception caught. " + e);
}
}
}
The output from this program
is the following:
Properties: rectangular
Events:
mouseWheel mouse mouseMotion component
hierarchyBounds focus
hierarchy propertyChange inputMethod key
Notice two things in the output. First, because ColorsBeanInfo overrides getPropertyDescriptors( ) such that the only property returned is rectangular, only the rectangular property is displayed. However, because getEventSetDescriptors( ) is not overridden by ColorsBeanInfo, design-pattern introspection is used, and all events are found, including those in Colors’ superclass, Canvas. Remember, if you don’t override one of the “get” methods defined by SimpleBeanInfo, then the default, design-pattern introspection is used. To observe the difference that ColorsBeanInfo makes, erase its class file and then run IntrospectorDemo again. This time it will report more properties.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.