Java Beans
Java Beans
Definition: A Java Bean is a reusable software component that can be visually manipulated in builder tools.
BeanInfo
Methods
JavaBean
Properties
Events
Component
Customizer
JAR
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
With beans, can "hook up" buttons to startAnimation and stopAnimation methods
When pressed, method called Builder tool does work
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"
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
Opening
Applet beans (like Juggler) begin executing immediately
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
import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable { public SimpleBean(){ setSize(60,40); setBackground(Color.red);}}
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