JavaBeans

Definition: What is a Bean?
If you have used Delphi, or Visual Basic, you are already familiar with the notion of a bean. The idea is the same; the programming language is different. A Java Bean is a reusable software component that works with Java. More specifically: a Java Bean is a reusable software component that can be visually manipulated in builder tools.

Definition: A Java Bean is a reusable software component that can be visually manipulated in builder tools.

• They are platform independent • Configuration setting of a bean can be saved in persistent storage and restored later • Bean may register and receive events from other object and can generate event sent to other objects (Bean communication) . run anywhere • The properties. and methods of a bean that are exposed to an application builder tool can be controlled • They are the interface of the bean. events.Advantage of Java Bean • Write once.

BeanInfo Methods JavaBean Properties Events Component Customizer JAR .

reuse and integrate components – Component assembler • Programmer who use defined components – Work on design of GUI and functionality • Do not need to know implementation – Just need to know services .Introduction • JavaBeans (beans) – Reusable software component model – Assemble predefined components • Create powerful applications and applets – Graphical programming and design environments • Builder tools • Support beans.

can "hook up" buttons to startAnimation and stopAnimation methods • When pressed. method called • Builder tool does work – Use previously defined. start and stop – With beans.Introduction • Example of bean concept – Have animation bean • Want two buttons. reusable components – Little or no code must be written • Component assembler can "connect the dots” .

Background currently selected (dashed box). following appears: Properties customizes selected bean. ToolBox has 16 sample JavaBeans Method Tracer displays debugging messages (not discussed) BeanBox window tests beans.BeanBox Overview • Use screen captures from Windows – Start application. .

BeanBox Overview – Initially. background selected • Customize in Properties box .

BeanBox Overview – Now. add JavaBean in BeanBox window • Click ExplicitButton bean in ToolBox window – Functions as a JButton • Click crosshair where center of button should appear • Change label to "Start the Animation" .

BeanBox Overview – Select button (if not selected) and move to corner • Position mouse on edges. move cursor appears • Drag to new location – Resize button • Put mouse in corner. resize cursor • Drag mouse to change size .

select Juggler and add to BeanBox • Animation begins immediately Properties for juggler.BeanBox Overview – Add another button (same steps) • "Stop the Animation" – Add animation bean • In ToolBox. .

BeanBox Overview – Now. "hook up" events from buttons • Start and stop animation – Edit menu • Access to events from beans that are an event source (bean can notify listener) • Swing GUI components are beans • Select "Stop the Animation" • Edit->Events->button push -> actionPerformed .

brings up EventTargetDialog – Shows public methods – Select stopJuggling .BeanBox Overview – Line appears from button to mouse • Target selector .target of event – Object with method we intend to call – Connect the dots programming • Click on Juggler.

BeanBox Overview – Event hookup complete • Writes new hookup/event adapter class • Object of class registered as actionListener fro button • Can click button to stop animation – Repeat for "Start the Animation" button • Method startAnimation .

BeanBox Overview • Save as design – Can reloaded into BeanBox later – Can have any file extension • Opening – Applet beans (like Juggler) begin executing immediately .

BeanBox Overview • Save as Java Applet – File->Make Applet • Stores .jar (Java Archive File) • Can rename and change directory .class file in .

html file. can hold beans – Archive property of <applet> tag • Comma separated list of . go to directory where applet saved – Should be .jar files for beans listed • Source code in AppletName_files directory .BeanBox Overview • To run applet – Go to command line. load into appletviewer • Background not yellow • BeanBox container not saved as part of applet • Applet is a container.jar files used • .

Program Output .

possibly synchronized to prevent more than one thread from calling method at a given time • Propertie X is exposed by public setX and getX methods • Boolean property may be exposed by isX method which returns a boolean value • The bean which may trigger event must provide addEventListener and removeEventListener mehods for other bean to register with it to be notified .Design Pattern • All beans should implement the Serializable interface so that the state can be saved and later restored • Methods must be made public • All exposed methods should be threadsafe.

gif files. BDK. class files.Deployment of Bean • All java classes can be converted to a bean • Bean is compressed and saved in the format of jar file which contains manifest file. and other information customization files • Sun NetBeans. JBuilder. Visual Café. Visual Age are the bean builder tools .

Criteria to be a bean • Can this piece of code be used in more than one area? • Can you quickly think of ways that this piece of code might be customized? • Is the purpose of this code easy to explain? • Does this code module contain all the info it needs to work itself? • Does it have good encapsulation? If you answer all “yes”. You should make the class a bean .

and download. . • JAR file makes it much easier to deliver. install.JAR file • JAR file allows you to efficiently deploy a set of classes and their associated resources. It is compressed.

..class Java-Bean: True . .tmp Name: SimpleBean.Manifest file Manifest.

Creating and extract a jar file • Create a jar file jar cfm simplebean.class • Extracting files from a jar file jar xf simplebean.jar manifest.jar .tmp *.

Steps for Creating a New Bean • • • • • • • Create a directory for the new bean Create the java bean source file(s) Compile the source file(s) Create a manifest file Generate a JAR file Start BDK Test Working-dir can be at <bdk>\demo where <bdk> is the installation dir for BDK .

}} . import java.Create bean source file SimpleBean.Serializable.40).red). public class SimpleBean extends Canvas implements Serializable { public SimpleBean(){ setSize(60. setBackground(Color.awt.io. import java.java package simplebean.*.

jar manifest.class Java-Bean: True • jar cfm .class [SimpleBean and colorsbean demo] . SimpleBean.tmp Name: SimpleBean..tmp simplebean\*.java • Edit a manifest file called manifest.\jars\simplebean.Compile and make jar file • Javac -d .

Introspection • Process of analyzing a bean to determine the capability • Allows application builder tool to present info about a component to software designer • Naming convention implicit method • BeanInfo class to explicitly infer info of a bean .

Design Pattern for Properties Property is a subset of a bean’s state which determines the appearance and behavior of the component Simple property Indexed Property Bound Property Constrained property • • • • .