Java Beans Definition What is a Bean If
Java. Beans
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.
Introduction • Java. Beans (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 start. Animation and stop. Animation 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" – More info about beans at • http: //java. sun. com/beans/
Bean. Box Overview • Bean. Box installation – Free utility from Java. Beans 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) • Bean. Box test container for Java. Beans – Preview how bean will be displayed – Not meant to be robust development tool
Bean. Box Overview • Use screen captures from Windows – Start application, following appears: Tool. Box has 16 sample Java. Beans Properties customizes selected bean. Method Tracer displays debugging messages (not discussed) Bean. Box window tests beans. Background currently selected (dashed box).
Bean. Box Overview – Initially, background selected • Customize in Properties box
Bean. Box Overview – Now, add Java. Bean in Bean. Box window • Click Explicit. Button bean in Tool. Box window – Functions as a JButton • Click crosshair where center of button should appear • Change label to "Start the Animation"
Bean. Box 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
Bean. Box Overview – Add another button (same steps) • "Stop the Animation" – Add animation bean • In Tool. Box, select Juggler and add to Bean. Box • Animation begins immediately Properties for juggler.
Bean. Box 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 -> action. Performed
Bean. Box 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 Event. Target. Dialog – Shows public methods – Select stop. Juggling
Bean. Box Overview – Event hookup complete • Writes new hookup/event adapter class • Object of class registered as action. Listener fro button • Can click button to stop animation – Repeat for "Start the Animation" button • Method start. Animation
Bean. Box Overview • Save as design – Can reloaded into Bean. Box later – Can have any file extension • Opening – Applet beans (like Juggler) begin executing immediately
Bean. Box Overview • Save as Java Applet – File->Make Applet • Stores. class file in. jar (Java Archive File) • Can rename and change directory
Bean. Box Overview • To run applet – Go to command line, go to directory where applet saved – Should be. html file, load into appletviewer • Background not yellow • Bean. Box 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 Applet. Name_files directory
1 <html> 2 <head> 3 <title>Test page for Our. Juggler as an APPLET</Title> 4 </head> 5 <body> 6 <h 1>Test for Our. Juggler as an APPLET</h 1> 7 This is an example of the use of the generated 8 Our. Juggler applet. 9 archives, one per JAR used in building the Applet HTML file Notice the Applet tag requires several 10 <p> 11 <applet 12 archive=". /Our. Juggler. jar, . /support. jar 13 , . /juggler. jar 14 , . /buttons. jar 15 " 16 code="Our. Juggler" 17 width=382 18 height=150 19 > 20 Trouble instantiating applet Our. Juggler!! 21 </applet> 1. archive
Program Output
Preparing a Class to Be a Java. Bean • 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 Bean. Box, can us predefined properties and events (background color, mouse events) – Code same as example in Chapter 16 except for two minor modifications
Preparing a Class to Be a Java. Bean • Minor modifications – Classes representing a bean placed into package • Compile with -d option • javac -d. Logo. Animator. java – ". " represents current directory (where to place package) – After compiled, wrap class into Java. Bean with jar utility » Next section – Implement Serializable interface • Persistence - save bean for future use • Can be serialized with Object. Output. Streams and Object. Input. Streams
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Fig. 25. 22: Logo. Animator. java // Animation bean package jhtp 3 beans; Java. Bean import import java. awt. *; java. awt. event. *; java. io. *; java. net. *; javax. swing. *; classes are normally packaged. Allows persistence, so Java. Bean can be written and saved. public class Logo. Animator extends JPanel implements Action. Listener, Serializable { protected Image. Icon images[]; protected int total. Images = 30, current. Image = 0, animation. Delay = 50; // 50 millisecond delay protected Timer animation. Timer; public Logo. Animator() { set. Size( get. Preferred. Size() ); images = new Image. Icon[ total. Images ]; URL url; for ( int i = 0; i < images. length; ++i ) { url = get. Class(). get. Resource( "deitel" + i + ". gif" ); images[ i ] = new Image. Icon( url ); } 1. package statement 2. implements Serializable
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 start. Animation(); } public void paint. Component( Graphics g ) { super. paint. Component( g ); if ( images[ current. Image ]. get. Image. Load. Status() == Media. Tracker. COMPLETE ) { g. set. Color( get. Background() ); g. draw. Rect( 0, 0, get. Size(). width, get. Size(). height ); images[ current. Image ]. paint. Icon( this, g, 0, 0 ); current. Image = ( current. Image + 1 ) % total. Images; } } public void action. Performed( Action. Event e ) { repaint(); } public void start. Animation() { if ( animation. Timer == null ) { current. Image = 0; animation. Timer = new Timer( animation. Delay, this ); animation. Timer. start(); }
62 63 else 64 if ( ! animation. Timer. is. Running() ) 65 66 // continue from last image displayed animation. Timer. restart(); } 67 68 public void stop. Animation() 69 { 70 animation. Timer. stop(); 71 } 72 73 public Dimension get. Minimum. Size() 74 { 75 76 return get. Preferred. Size(); } 77 78 public Dimension get. Preferred. Size() 79 { 80 81 return new Dimension( 160, 80 ); } 82 83 84 85 public static void main( String args[] ) { Logo. Animator anim = new Logo. Animator(); 86 87 JFrame app = new JFrame( "Animator test" ); 88 app. get. Content. Pane(). add( anim, Border. Layout. CENTER ); 89
90 91 92 93 94 95 96 97 98 99 100 app. add. Window. Listener( new Window. Adapter() { public void window. Closing( Window. Event e ) { System. exit( 0 ); } } ); 101 app. show(); 102 app. set. Size( anim. get. Preferred. Size(). width + 10, anim. get. Preferred. Size(). height + 30 ); } 103 } Program Output
Creating a Java. Bean: Java Archive Files and the jar Utility • Place class in Java Archive file (JAR) – Create text file manifest. tmp • Manifest file describes contents of JAR file • jar utility uses manifest. tmp – Creates MANIFEST. MF in META-INF directory – Used by development environments – Can execute application from JAR file with java interpreter • Specify class with main
Creating a Java. Bean: Java Archive Files and the jar Utility 1 Main-Class: jhtp 3 beans. Logo. Animator 2 3 Name: jhtp 3 beans/Logo. Animator. class 4 Java-Bean: True – Manifest file for Logo. Animator • Specify class with main, runs bean as application – java -jar Logo. Animator. jar • Run application from bean • Interpreter looks at manifest file – Executes main of Main-Class java. -cp Logo. Animator. jar jhtp 3 beans. Logo. Animator – cp - class path, JAR file to look for classes – Followed by application class (explicit name, with package)
Creating a Java. Bean: Java Archive Files and the jar Utility 1 Main-Class: jhtp 3 beans. Logo. Animator 2 3 Name: jhtp 3 beans/Logo. Animator. class 4 Java-Bean: True – 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 Java. Bean • Possible to have non-Java. Bean in JAR file • Used to support Java. Beans – Each class separated by blank line • Java-Bean: immediately follows Name:
Creating a Java. Bean: Java Archive Files and the jar Utility • Create JAR file – jar utility at command line jar cfm Logo. Animator. jar manifest. tmp jhtp 3 beans*. * – Options • c - creating JAR file • f - indicates next argument is name of file • m - next argument manifest. tmp file – Used to create MANIFEST. MF – Next, list files to be included in JAR file • Directory structure of JAR file should match manifest. tmp
Creating a Java. Bean: Java Archive Files and the jar Utility • To confirm files were archived – jar tvf Logo. Animator. jar – Options • t - list table of contents • v - verbose mode • f - next argument is JAR file to use – Execute Logo. Animator application java -jar Logo. Animator. jar
0 Sun Mar 14 11: 36: 16 EST 1999 META-INF/ 163 Sun Mar 14 11: 36: 16 EST 1999 META-INF/MANIFEST. MF 4727 Thu Feb 15 00: 37: 04 EST 1996 jhtp 3 beans/deitel 0. gif 4858 Thu Feb 15 00: 39: 32 EST 1996 jhtp 3 beans/deitel 1. gif 4374 Thu Feb 15 00: 55: 46 EST 1996 jhtp 3 beans/deitel 10. gif 4634 Thu Feb 15 00: 56: 52 EST 1996 jhtp 3 beans/deitel 11. gif 4852 Thu Feb 15 00: 58: 00 EST 1996 jhtp 3 beans/deitel 12. gif 4877 Thu Feb 15 00: 59: 10 EST 1996 jhtp 3 beans/deitel 13. gif 4926 Thu Feb 15 01: 00: 20 EST 1996 jhtp 3 beans/deitel 14. gif 4765 Thu Feb 15 01: 32 EST 1996 jhtp 3 beans/deitel 15. gif 4886 Thu Feb 15 01: 05: 16 EST 1996 jhtp 3 beans/deitel 16. gif 4873 Thu Feb 15 01: 06: 12 EST 1996 jhtp 3 beans/deitel 17. gif 4739 Thu Feb 15 01: 07: 18 EST 1996 jhtp 3 beans/deitel 18. gif 4566 Thu Feb 15 01: 08: 24 EST 1996 jhtp 3 beans/deitel 19. gif 4819 Thu Feb 15 00: 41: 06 EST 1996 jhtp 3 beans/deitel 2. gif 4313 Thu Feb 15 01: 09: 48 EST 1996 jhtp 3 beans/deitel 20. gif 3910 Thu Feb 15 01: 10: 46 EST 1996 jhtp 3 beans/deitel 21. gif 3076 Thu Feb 15 01: 12: 02 EST 1996 jhtp 3 beans/deitel 22. gif 3408 Thu Feb 15 01: 13: 16 EST 1996 jhtp 3 beans/deitel 23. gif 4039 Thu Feb 15 01: 14: 06 EST 1996 jhtp 3 beans/deitel 24. gif 4393 Thu Feb 15 01: 15: 02 EST 1996 jhtp 3 beans/deitel 25. gif 4626 Thu Feb 15 01: 16: 06 EST 1996 jhtp 3 beans/deitel 26. gif 4852 Thu Feb 15 01: 17: 18 EST 1996 jhtp 3 beans/deitel 27. gif 4929 Thu Feb 15 01: 18 EST 1996 jhtp 3 beans/deitel 28. gif 4914 Thu Feb 15 01: 19: 16 EST 1996 jhtp 3 beans/deitel 29. gif 4769 Thu Feb 15 00: 42: 52 EST 1996 jhtp 3 beans/deitel 3. gif 4617 Thu Feb 15 00: 43: 54 EST 1996 jhtp 3 beans/deitel 4. gif 4335 Thu Feb 15 00: 47: 14 EST 1996 jhtp 3 beans/deitel 5. gif 3967 Thu Feb 15 00: 49: 40 EST 1996 jhtp 3 beans/deitel 6. gif 3200 Thu Feb 15 00: 58 EST 1996 jhtp 3 beans/deitel 7. gif 3393 Thu Feb 15 00: 52: 32 EST 1996 jhtp 3 beans/deitel 8. gif 4006 Thu Feb 15 00: 53: 48 EST 1996 jhtp 3 beans/deitel 9. gif 420 Sun Mar 14 11: 36: 16 EST 1999 jhtp 3 beans/Logo. Animator$1. class 3338 Sun Mar 14 11: 36: 16 EST 1999 jhtp 3 beans/Logo. Animator. class jar tvf Logo. Animator. jar
Adding Beans to the Bean. Box • Using Beans – Logo. Animator is wrapped in a JAR file as a Java. Bean • Can use in Bean. Box – Two ways to load bean • Put JAR file in BDK 1. 1jars directory – Loaded into toolbox • Use File -> Load. Jar
Adding Beans to the Bean. Box • To add to design area – Click bean in Tool. Box – Click crosshair where bean should appear
Adding Beans to the Bean. Box • Properties window – Shows properties of Logo. Animator • Properties inherited from JPanel
Connecting Beans with Events in the Bean. Box • Connecting Beans with events – Logo. Animator has methods stop. Animation and start. Animation – Connect two Explicit. Buttons to Logo. Animator • Change label • Edit -> Events -> button push -> action. Performed
Adding Properties to a Java. Bean • Add animation. Delay property – Control animation speed – Extend Logo. Animator and create Logo. Animator 2 – Read/write property of bean • Defined as set/get method pair of format: public void set. Property. Name( Data. Type value ) public Data. Type get. Property. Name() • Property set and get methods • If using boolean, use is. Property. Name() instead of get – We use set. Animation. Delay and get. Animation. Delay
1 // Fig. 25. 30: Logo. Animator 2. java 2 // Animation bean with animation. Delay property 3 package jhtp 3 beans; 4 5 import java. awt. *; 6 import java. awt. event. *; 7 import javax. swing. *; 8 9 public class Logo. Animator 2 extends Logo. Animator { 10 // the following two methods are 11 // for the animation. Delay property 12 public void set. Animation. Delay( int value ) 13 { 14 animation. Delay = value; 15 16 17 animation. Timer. set. Delay( animation. Delay ); } 18 19 public int get. Animation. Delay() 20 { 21 22 return animation. Timer. get. Delay(); } 23 24 public static void main( String args[] ) 25 { 26 Logo. Animator 2 anim = new Logo. Animator 2(); 1. extends Logo. Animator 2. set. Animation. Delay 3. get. Animation. Delay Methods must follow format.
27 28 JFrame app = new JFrame( "Animator test" ); 29 app. get. Content. Pane(). add( anim, Border. Layout. CENTER ); 30 31 app. add. Window. Listener( 32 new Window. Adapter() { 33 34 public void window. Closing( Window. Event e ) { 35 System. exit( 0 ); 36 } 37 } 38 ); 39 40 app. set. Size( anim. get. Preferred. Size(). width + 10, 41 anim. get. Preferred. Size(). height + 30 ); 42 43 44 } app. show(); }
Adding Properties to a Java. Bean • Properties – When builder tool examines bean, looks for pairs of set/get methods • Introspection • If found, used as property • Creating bean – Must wrap Logo. Animator 2 class – Compile: javac -d. Logo. Animator 2. java – Create manifest. tmp 1 Main-Class: jhtp 3 beans. Logo. Animator 2 2 3 Name: jhtp 3 beans/Logo. Animator 2. class 4 Java-Bean: True
Adding Properties to a Java. Bean • Creating bean – Package into JAR file jar cfm Logo. Animator 2. jar manifest. tmp jhtp 3 beans*. * – Load Logo. Animator 2 bean, can change animation. Delay property
Creating a Java. Bean with a Bound Property • Bound property – Other objects notified when it changes – Use event handling • Registered Property. Change. Listeners notified when value changes – java. beans package • Class Property. Change. Event, Property. Change. Listener • Example – Create custom GUI component Slider. Field. Panel • Has a JText. Field linked to a JSlider • Changes affect them both
Creating a Java. Bean with a Bound Property • Example – Want to link this to Logo. Animator 2 • Want Slider. Field. Panel to control animation speed 3 package jhtp 3 beans; – Use same package 12 public class Slider. Field. Panel extends JPanel 13 implements Serializable { – Subclass of JPanel, can add JSlider and JText. Field
Creating a Java. Bean with a Bound Property 25 change. Support = new Property. Change. Support( this ); – Create object • argument (this) - source of Property. Change. Event 40 41 42 43 44 45 46 47 slider. add. Change. Listener( new Change. Listener() { public void state. Changed( Change. Event e ) { set. Current. Value( slider. get. Value() ); } } ); – Register Change. Listener • When changed, calls set. Current. Value • Notifies registered Property. Change. Listeners
Creating a Java. Bean with a Bound Property 61 62 63 64 65 66 67 68 69 70 71 public void add. Property. Change. Listener( Property. Change. Listener listener ) { change. Support. add. Property. Change. Listener( listener ); } public void remove. Property. Change. Listener( Property. Change. Listener listener ) { change. Support. remove. Property. Change. Listener( listener ); } – Help register Change. Listeners • Supply listener interface and event class • Must have methods for adding/removing listeners • Use this naming for bound property events
Creating a Java. Bean with a Bound Property 106 107 112 113 114 public void set. Current. Value( int current ) { change. Support. fire. Property. Change( "current. Value", new Integer( old. Value ), new Integer( current. Value ) ); – current. Value - bound property • set. Current. Value, get. Current. Value • When changes, must notify registered Property. Change. Listeners – Method fire. Property. Change( property. Name, old. Value, new. Value ); • Of Property. Change. Support • Notifies registered listeners
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Fig. 25. 33: Slider. Field. Panel. java // A subclass of JPanel containing a JSlider and a JText. Field package jhtp 3 beans; import import javax. swing. *; javax. swing. event. *; java. io. *; java. awt. event. *; java. beans. *; 1. package 1. 1 extends JPanel 1. 2 Declarations public class Slider. Field. Panel extends JPanel implements Serializable { private JSlider slider; private JText. Field field; private Box box. Container; private int current. Value; Create new object, 1. 3 Constructor used to register change // object to support bound property changes listeners. private Property. Change. Support change. Support; public Slider. Field. Panel() { // create Property. Change. Support for bound properties change. Support = new Property. Change. Support( this ); slider = new JSlider( Swing. Constants. HORIZONTAL, 1, 100, 1 ); field = new JText. Field( String. value. Of( slider. get. Value() ), 5 ); 1. 4 Property. Change Support 1. 5 GUI
31 32 box. Container = new Box( Box. Layout. X_AXIS ); 33 box. Container. add( slider ); 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 box. Container. add( Box. create. Horizontal. Strut( 5 ) ); box. Container. add( field ); 1. 6 Event handlers set. Layout( new Border. Layout() ); add( box. Container ); slider. add. Change. Listener( new Change. Listener() { public void state. Changed( Change. Event e ) { set. Current. Value( slider. get. Value() ); } } Call method set. Current. Value ); when value changes. field. add. Action. Listener( new Action. Listener() { public void action. Performed( Action. Event e ) { set. Current. Value( Integer. parse. Int( field. get. Text() ) ); } } ); }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 // methods for adding and removing Property. Change. Listeners public void add. Property. Change. Listener( Property. Change. Listener listener ) { change. Support. add. Property. Change. Listener( listener ); } 2. add. Property. Change Methods to support registration. Listener Methods must have this naming. public void remove. Property. Change. Listener( Property. Change. Listener listener ) { change. Support. remove. Property. Change. Listener( listener ); } // property minimum. Value public void set. Minimum. Value( int min ) { slider. set. Minimum( min ); if ( slider. get. Value() < slider. get. Minimum() ) { slider. set. Value( slider. get. Minimum() ); field. set. Text( String. value. Of( slider. get. Value() ) ); } } public int get. Minimum. Value() { return slider. get. Minimum(); } 3. remove. Property Change. Listener
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 // property maximum. Value public void set. Maximum. Value( int max ) { slider. set. Maximum( max ); if ( slider. get. Value() > slider. get. Maximum() ) { slider. set. Value( slider. get. Maximum() ); field. set. Text( String. value. Of( slider. get. Value() ) ); } } public int get. Maximum. Value() { return slider. get. Maximum(); } // property current. Value public void set. Current. Value( int current ) { int old. Value = current. Value; current. Value = current; slider. set. Value( current. Value ); field. set. Text( String. value. Of( current. Value ) ); change. Support. fire. Property. Change( "current. Value", new Integer( old. Value ), new Integer( current. Value ) ); } public int get. Current. Value() { return slider. get. Value(); } 4. set. Current. Value 4. 1 fire. Property. Change 5. get. Current. Value Pass name, old value, and new value. This notifies registered listeners of change.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 } // property field. Width public void set. Field. Width( int cols ) { field. set. Columns( cols ); box. Container. validate(); } public int get. Field. Width() { return field. get. Columns(); } public Dimension get. Minimum. Size() { return box. Container. get. Minimum. Size(); } public Dimension get. Preferred. Size() { return box. Container. get. Preferred. Size(); } 6. set/get methods
Creating a Java. Bean with a Bound Property • Create bean – Compile: javac -d. Slider. Field. Panel. java – Manifest file (manifest. tmp) 1 Name: jhtp 3 beans/Slider. Field. Panel. class 2 Java-Bean: True • No Main-Class: because not an application – Archive in JAR file: jar cfm Slider. Field. Panel. jar manifest. tmp jhtp 3 beans*. *
Creating a Java. Bean with a Bound Property • Usage – Add Slider. Field. Panel to Bean. Box with Juggler • Set maximum. Value to 1000, current. Value to 125 (default animations speed for Juggler) – Go to Edit->Bind property. . . • Click current. Value
Creating a Java. Bean with a Bound Property • Usage – Red target selector line appears • Connect to Juggler and click – Property. Name. Dialog • Shows target properties with same data type as bound property • Select animation. Rate (only listed property) • animation. Rate bound to current. Value
Specifying the Bean. Info Class for a Java. Bean • 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 Bean. Info • Bean. Info class has same name as bean, ends in Bean. Info – Slider. Field. Panel. Bean. Info. java • Placed in same package as bean
Specifying the Bean. Info Class for a Java. Bean 7 public class Slider. Field. Panel. Bean. Info extends Simple. Bean. Info { – Simple. Bean. Info • Default implementation of Bean. Info • Selectively override methods 8 9 public final static Class bean. Class = Slider. Field. Panel. class; – Class • Allows program to refer to class definition • Refers to class that will be searched for features described in bean. Info class • final - will not be modified • static - only one instance needed
Specifying the Bean. Info Class for a Java. Bean 11 12 14 15 public Property. Descriptor[] get. Property. Descriptors() { Property. Descriptor field. Width = new Property. Descriptor( "field. Width", bean. Class ); – get. Property. Descriptors (overridden) • Returns array of Property. Descriptor objects – Each describes property – new Property. Descriptor( "property. Name", bean. Class ) • Property must have named set and get methods 26 current. Value. set. Bound( true ); – set. Bound( true ) • Specifies property is bound
Specifying the Bean. Info Class for a Java. Bean 39 public int get. Default. Property. Index() 40 { 41 42 return 1; } – get. Default. Property. Index (overridden) • Returns array index of default property • In this case, current. Value 44 public Event. Set. Descriptor[] get. Event. Set. Descriptors() { – get. Event. Set. Descriptors • Returns array of Event. Set. Descriptor objects • Describes events supported by bean
Specifying the Bean. Info Class for a Java. Bean 46 47 48 49 50 Event. Set. Descriptor changed = new Event. Set. Descriptor( bean. Class, "property. Change", java. beans. Property. Change. Listener. class, "property. Change"); – Constructor arguments • 1: Class object, represents event source • 2: String, event set name – mouse includes mouse. Pressed, mouse. Clicked. . . – We use property. Change • 3: Class object, implemented event listener interface – Creates anonymous object of class • 4: String, name of listener method called when event occurs
Specifying the Bean. Info Class for a Java. Bean 52 53 changed. set. Display. Name( "Slider. Field. Panel value changed" ); – Class Event. Set. Descriptor – Method set. Display. Name( string. Name ) • Specifies name for event set in builder tool
1 2 // Fig. 25. 38: Slider. Field. Panel. Bean. Info. java Same package // The Bean. Info class for Slider. Field. Panel 3 package jhtp 3 beans; 4 5 import java. beans. *; as bean. Extend Simple. Bean. Info class, with default implementation of Bean. Info interface. 1. package 6 11 1. 1 import public final static Class bean. Class = Create Class object, provides Slider. Field. Panel. class; access to class 1. 2 definition. extends Specify properties that can be Simple. Bean. Info by builder. public Property. Descriptor[]exposed get. Property. Descriptors() 12 { 7 public class Slider. Field. Panel. Bean. Info extends Simple. Bean. Info { 8 9 10 13 14 15 16 17 18 19 20 21 22 23 ); 24 1. 3 Class try { Property. Descriptor field. Width = new Property. Descriptor( "field. Width", bean. Class ); Property. Descriptor current. Value = new Property. Descriptor( "current. Value", bean. Class ); 2. 1 set. Bound Property. Descriptor maximum. Value = new Property. Descriptor( "maximum. Value", bean. Class ); Property. Descriptor minimum. Value = new Property. Descriptor( "minimum. Value", bean. Class Specify bound property. 25 // ensure Property. Change. Event occurs for this property 26 current. Value. set. Bound( true ); 27 2. get. Property Descriptors
28 29 30 31 32 33 34 } catch ( Introspection. Exception ie ) { throw new Runtime. Exception( ie. to. String() ); 35 } 36 Property. Descriptor descriptors[] = { field. Width, current. Value, maximum. Value, minimum. Value }; return descriptors; 3. get. Default. Property Index Get array index of default property (current. Value). } 37 38 // the index for the current. Value property 39 40 public int get. Default. Property. Index() { 41 42 return 1; } 45 46 47 public Event. Set. Descriptor[] get. Event. Set. Descriptors() { try { Event. Set. Descriptor changed = "property. Change", 49 java. beans. Property. Change. Listener. class, 50 "property. Change"); 51 changed. set. Display. Name( "Slider. Field. Panel value changed" ); 54 55 Describes events supported by bean. new Event. Set. Descriptor( bean. Class, 48 52 53 4. 1 Constructor 4. 2 set. Display. Name 43 44 4. get. Event. Set Descriptors Event. Set. Descriptor[] descriptors = { changed }; Set title of event in builder.
56 57 return descriptors; 58 59 } catch (Introspection. Exception e) { 60 61 62 throw new Runtime. Exception(e. to. String()); } } 63 } Program Output Only the specified properties are listed.
Java. Beans World Wide Web Resources • 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
- Slides: 62