0% found this document useful (0 votes)
38 views3 pages

Experiment 9

Uploaded by

Rohit Majumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Experiment 9

Uploaded by

Rohit Majumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT 9

AIM: Develop a program to implement Java Beans.

JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a Java class
that should follow the following conventions:

 Must implement Serializable.


 It should have a public no-arg constructor.
 All properties in java bean must be private with public getters and setter methods.

Writing Your Own Javabeans


The JavaBeans APIs covers five aspects:
1. Properties: represent the attributes of a component.
2. Event Handling: allows beans to communicate with each other (JavaBean uses JDK
1.1 AWT event-delegation model).
3. Persistence: allows beans' internal states to be stored and later restored.
4. Introspection: allows Application Builder tool to analyze beans.
5. Application Builder Tool: for composing applications from Javabeans components.
First Javabean - A LightBulb
package elect;
import java.awt.*;
import java.io.Serializable;

public class LightBulb extends Canvas implements Serializable {

public LightBulb() { // constructor


setSize(50,50);
setBackground(Color.GRAY);
}

// Properties
private static final Color COLOR_OFF = Color.BLACK;
private Color color = Color.ORANGE; // property with a default value
public Color getColor() { return color; } // getter
public void setColor(Color color) { this.color = color; } // setter

boolean on = false; // property with a default value


public boolean isOn() { return on; } // getter for boolean
public void setOn(boolean on) { this.on = on; } // setter

// Override the paint() method to draw the LightBulb


public void paint(Graphics g) {
if (on) g.setColor(color);
else g.setColor(COLOR_OFF);
g.fillOval(10, 10, 30, 30);
}

public void switchOn() { // switch on the Light


on = true;
repaint();
}

public void switchOff() { // switch off the Light


on = false;
repaint();
}

public void toggle() { // If on turns off; else turns on


on = !on;
repaint();
}
}
compile the all the source files in the package and place the classes in $CLASS_BASEDIR:
> cd $SRC_BASEDIR // set current directory at source base directory
> javac –d $CLASS_BASEDIR elect\*.java // compile the entire package and place in
class base directory
Create a manifest (to be included into the jar file) called " $CLASS_BASEDIR\manifest.Bulb" as
follows:
Manifest-Version: 1.0
Name: elect/LightBulb.class
Java-Bean: True

> cd $CLASS_BASEDIR
> jar cmfv manifest.Bulb $JAR_BASEDIR/lightbulb.jar elect\LightBulb.class

Testing the LightBulb Bean (with BDK)

1. Start the BDK beanbox (by executing "$BDK\beanbox\run.bat").

2. From the "Beanbox" window, choose "File" ⇒ "LoadJar" ⇒ Chose "lightbulb.jar". You shall
see LightBulb appears at the bottom of the "ToolBox" window.

3. Select LightBulb from the "ToolBox" window, and place it into the "Beanbox". Observe that
the "Property" window shows the two properties defined in this bean: color and on. Try
changing these properties and observe the result. The "Property" window also shows the
properties inherited from the superclasses.

4. Select "OurButton" bean (a demo bean provided by BDK) from "Toolbox" and place it inside
the "Beanbox". In the "Property" window, change the "label" from "press" to "toggle".

5. Focus on "OurButton", choose "Edit" from menu ⇒ "Events" ⇒ "mouse" ⇒ "mouseClicked"


and place it onto the LightBulb. In the "EventTargetDialog", select method toggle() as the
event handler.

6. Click the "toggle" button and observe the result.

7. Focus on LightBulb, choose "Edit" from menu ⇒ "Events" ⇒ "mouse" ⇒ "mouseClicked" and
place it back to LightBulb. In the "EventTargetDialog", select method toggle() as the event
handler.
8. Click the LightBulb and observe the effect.

You might also like