Professional Documents
Culture Documents
• 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
• BeanBox installation
– Free utility from JavaBeans Development Kit (BDK)
http://java.sun.com/beans/software/index.html
• Windows, Solaris, and platform independent versions
• In Windows version, minor bug
– Do not install in directory with spaces in name
• To run, go to install directory, beanbox subdirectory, load
run.bat (or run.sh)
• BeanBox test container for JavaBeans
– Preview how bean will be displayed
– Not meant to be robust development tool
• Save as design
– Can reloaded into BeanBox later
– Can have any file extension
• Opening
– Applet beans (like Juggler) begin executing immediately
• 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
• Example
– Animation of Deitel & Associates, Inc. logo (as in Chapter
16)
• Before, stand-alone application in a JFrame
• Now, stand-alone application and as bean
– Application extends JPanel
• When loaded into BeanBox, can us predefined properties and
events (background color, mouse events)
– Code same as example in Chapter 16 except for two minor
modifications
• Minor modifications
– Classes representing a bean placed into package
• Compile with -d option
• javac -d . LogoAnimator.java
– "." represents current directory (where to place package)
– After compiled, wrap class into JavaBean with jar utility
» Next section
– Implement Serializable interface
• Persistence - save bean for future use
• Can be serialized with ObjectOutputStreams and
ObjectInputStreams
– Name: name of file with bean class (full package and class
name)
• Dots . used in package named replaced with /
– Java-Bean: true - file is a JavaBean
• Possible to have non-JavaBean in JAR file
• Used to support JavaBeans
– Each class separated by blank line
• Java-Bean: immediately follows Name:
• Using Beans
– LogoAnimator is wrapped in a JAR file as a JavaBean
• Can use in BeanBox
– Two ways to load bean
• Put JAR file in BDK1.1\jars directory
– Loaded into toolbox
• Use File -> LoadJar
• Properties window
– Shows properties of LogoAnimator
• Properties inherited from JPanel
• Properties
– When builder tool examines bean, looks for pairs of set/get
methods
• Introspection
• If found, used as property
• Creating bean
– Must wrap LogoAnimator2 class
– Compile: javac -d . LogoAnimator2.java
– Create manifest.tmp
1 Main-Class: jhtp3beans.LogoAnimator2
2
3 Name: jhtp3beans/LogoAnimator2.class
4 Java-Bean: True
• Creating bean
– Package into JAR file
jar cfm LogoAnimator2.jar manifest.tmp jhtp3beans\*.*
– Load LogoAnimator2 bean, can change
animationDelay property
• Bound property
– Other objects notified when it changes
– Use event handling
• Registered PropertyChangeListeners notified when
value changes
– java.beans package
• Class PropertyChangeEvent,
PropertyChangeListener
• Example
– Create custom GUI component SliderFieldPanel
• Has a JTextField linked to a JSlider
• Changes affect them both
• Example
– Want to link this to LogoAnimator2
• Want SliderFieldPanel to control animation speed
3 package jhtp3beans;
– Create object
• argument (this) - source of PropertyChangeEvent
40 slider.addChangeListener(
41 new ChangeListener() {
42 public void stateChanged( ChangeEvent e )
43 {
44 setCurrentValue( slider.getValue() );
45 }
46 }
47 );
– Register ChangeListener
• When changed, calls setCurrentValue
• Notifies registered PropertyChangeListeners
2000 Prentice Hall, Inc. All rights reserved.
Creating a JavaBean with a Bound Property
• Create bean
– Compile: javac -d . SliderFieldPanel.java
– Manifest file (manifest.tmp)
1 Name: jhtp3beans/SliderFieldPanel.class
2 Java-Bean: True
• Usage
– Add SliderFieldPanel to BeanBox with Juggler
• Set maximumValue to 1000, currentValue to 125
(default animations speed for Juggler)
– Go to Edit->Bind property...
• Click currentValue
• Usage
– Red target selector line appears
• Connect to Juggler and click
– PropertyNameDialog
• Shows target properties with same data type as bound property
• Select animationRate (only listed property)
• animationRate bound to currentValue
• Introspection mechanism
– Used by builder tool
– Exposes bean's properties, methods, and events if
programmer uses design patterns
• I.e., special naming conventions
– Use classes in java.lang.reflect for introspection
– Can customize which methods/events/properties available
• Create class that implements interface BeanInfo
• BeanInfo class has same name as bean, ends in BeanInfo
– SliderFieldPanelBeanInfo.java
• Placed in same package as bean
– SimpleBeanInfo
• Default implementation of BeanInfo
• Selectively override methods
– Class Class
• Allows program to refer to class definition
• Refers to class that will be searched for features described in
beanInfo class
• final - will not be modified
• static - only one instance needed
– getPropertyDescriptors (overridden)
• Returns array of PropertyDescriptor objects
– Each describes property
– new PropertyDescriptor( "propertyName", beanClass )
• Property must have named set and get methods
26 currentValue.setBound( true );
– setBound( true )
• Specifies property is bound
– getDefaultPropertyIndex (overridden)
• Returns array index of default property
• In this case, currentValue
44 public EventSetDescriptor[] getEventSetDescriptors() {
– getEventSetDescriptors
• Returns array of EventSetDescriptor objects
• Describes events supported by bean
– Constructor arguments
• 1: Class object, represents event source
• 2: String, event set name
– mouse includes mousePressed, mouseClicked...
– We use propertyChange
• 3: Class object, implemented event listener interface
– Creates anonymous object of class
• 4: String, name of listener method called when event occurs
52 changed.setDisplayName(
53 "SliderFieldPanel value changed" );
– Class EventSetDescriptor
– Method setDisplayName( stringName )
• Specifies name for event set in builder tool
Program Output
• Web resources
– http://java.sun.com/beans/
• Download Beans Development Kit, docs
– http://java.sun.com/beans/spec.html
• Specifications
– http://java.sun.com/beans/tools.html
• Beans-enabled development tools
– http://java.sun.com/beans/directory/
• Searchable directory of beans
– http://java.sun.com/products/hotjava/bean/
index.html
• Evaluation version of bean with HTML rendering