0% found this document useful (0 votes)
118 views

Java Beans

The document discusses JavaBeans, which are reusable software components that can be visually manipulated in builder tools. It defines what a JavaBean is, lists advantages like write once run anywhere, and discusses BeanInfo and how beans are used with builder tools like BeanBox to visually connect components without writing code.

Uploaded by

Anshy Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Java Beans

The document discusses JavaBeans, which are reusable software components that can be visually manipulated in builder tools. It defines what a JavaBean is, lists advantages like write once run anywhere, and discusses BeanInfo and how beans are used with builder tools like BeanBox to visually connect components without writing code.

Uploaded by

Anshy Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

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.

Advantage of Java Bean


Write once, run anywhere The properties, events, and methods of a bean that are exposed to an application builder tool can be controlled They are the interface of the bean. 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)

BeanInfo

Methods

JavaBean
Properties

Events

Component

Customizer

JAR

Introduction JavaBeans (beans)


Reusable software component model Assemble predefined components
Create powerful applications and applets

Graphical programming and design environments


Builder tools Support beans, 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 Example of bean concept


Have animation bean
Want two buttons, start and stop

With beans, can "hook up" buttons to startAnimation and stopAnimation methods
When pressed, method called Builder tool does work

Use previously defined, reusable components Little or no code must be written


Component assembler can "connect the dots

BeanBox Overview Use screen captures from Windows


Start application, following appears:
Properties customizes selected bean.

ToolBox has 16 sample JavaBeans

Method Tracer displays debugging messages (not discussed) BeanBox window tests beans. Background currently selected (dashed box).

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

BeanBox Overview
Add another button (same steps)
"Stop the Animation"

Add animation bean


In ToolBox, select Juggler and add to BeanBox Animation begins immediately

Properties for juggler.

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

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, brings up EventTargetDialog Shows public methods Select stopJuggling

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 .class file in .jar (Java Archive File)
Can rename and change directory

BeanBox Overview To run applet


Go to command line, go to directory where applet saved Should be .html file, load into appletviewer
Background not yellow BeanBox container not saved as part of applet Applet is a container, can hold beans

Archive property of <applet> tag


Comma separated list of .jar files used .jar files for beans listed Source code in AppletName_files directory

Program Output

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, 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

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, class files, gif files, and other information customization files Sun NetBeans, BDK, Visual Caf, JBuilder, 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

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

Manifest file
Manifest.tmp

Name: SimpleBean.class Java-Bean: True ...

Creating and extract a jar file


Create a jar file

jar cfm simplebean.jar manifest.tmp *.class


Extracting files from a jar file jar xf simplebean.jar

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

Create bean source file SimpleBean.java


package simplebean;

import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable { public SimpleBean(){ setSize(60,40); setBackground(Color.red);}}

Compile and make jar file


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

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 beans state which determines the appearance and behavior of the component Simple property Indexed Property Bound Property Constrained property

You might also like