Chapter 6 Java Beans Component Model Outline 6
Chapter 6: Java Beans Component Model Outline 6. 1 6. 2 6. 3 6. 4 6. 5 6. 6 6. 7 6. 8 Introduction Using Beans in Forte for Java Community Edition Preparing a Class to be a Java. Bean Creating a Java. Bean: Java Archive Files Java. Bean Properties Bound Properties Indexed Properties and Custom Events Customizing Java. Beans for Builder Tools 6. 8. 1 Property. Editors 6. 8. 2 Customizers 2002 Prentice Hall. All rights reserved.
6. 1 Introduction • Java. Beans – Used for rapid application development • Assembling predefined software components – Used by a component assembler – Communicate through well-defined services (i. e. , methods) – Promote code reuse 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition • Forte for Java Community Edition – Integrated development environment – Provides builder tool for assembling Java. Beans • Installing and manipulating beans – Beans must be installed before being manipulated – After being installed, beans can be inserted in Forms 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 1 2002 Prentice Hall. All rights reserved. Forte for Java Community Edition 2. 0.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 2 2002 Prentice Hall. All rights reserved. Install New Java. Bean. . . menu item.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 3 2002 Prentice Hall. All rights reserved. Install Java. Bean dialog.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 4 Select Java. Bean and Palette Category dialogs. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Component Palette Fig. 6. 5 Beans tab in the Component Palette and tooltip for Logo. Animator Java. Bean. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 6 2002 Prentice Hall. All rights reserved. Filesystems tab in the Explorer window.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 7 Development directory selected in Explorer window. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 8 2002 Prentice Hall. All rights reserved. New. . . menu item.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 9 2002 Prentice Hall. All rights reserved. New. . . - Template Chooser dialog.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 10 GUI Editing tab of Forte. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 11 Component Inspector and Form windows 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 12 Source Editor window. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 13 Beans tab of the Component Palette. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 14 Logo. Animator icon. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 15 Logo. Animator animation in the Form window. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 16 Component Inspector with Logo. Animator Properties sheet. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 17 Component Inspector drop down-menu for the background property. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 18 Changing background color of Logo. Animator. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 19 Animation. Window selected in Explorer. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 20 Selecting Flow. Layout in the Explorer menu. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 21 Swing tab of the Component Palette. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 22 JButton icon in the Component Palette. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 23 Adding a JButton to Animation. Window. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 24 Editing text property of JButton. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 25 Component Palette Selection mode. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 26 Component Palette Connection mode. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 27 Select Connection mode. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 28 Connecting JButton and Logo. Animator. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 29 Connection Wizard dialog. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 30 Select action. Performed event. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 31 Selecting method start. Animation for the target component. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 32 Select Execute from Explorer menu. 2002 Prentice Hall. All rights reserved.
6. 2 Using Beans in Forte for Java Community Edition (cont. ) Fig. 6. 33 Animation. Window running in Forte. 2002 Prentice Hall. All rights reserved.
6. 3 Preparing a Class to be a Java. Bean • Use our Logo. Animator bean – Examine Logo. Animator. class 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 34: Logo. Animator. java 2 // Logo. Animator is a Java. Bean containing an animated logo. 3 package com. deitel. advjhtp 1. beans; 4 5 // Java core packages Logo. Animator extends JPanel, 6 import java. awt. *; making it a GUI component 7 import java. awt. event. *; 8 import java. io. *; 9 import java. net. *; All Java. Beans must 10 11 // Java extension packages implements interface 12 import javax. swing. *; Serializable 13 Fig. 6. 34 Definition 14 public class Logo. Animator extends JPanel of class Logo 15 implements Action. Listener, Serializable { Array of Image. Icons, which 1). Animator (part 16 17 protected Image. Icon images[]; contains 30 PNG image that 18 protected int total. Images = 30, current. Image; comprise the animated logo Line 14 19 protected Timer animation. Timer; 20 21 // load images and start animation Line 15 22 public Logo. Animator() 23 { 24 images = new Image. Icon[ total. Images ]; Lines 17 -33 25 26 URL url; 27 28 // load animation frames 29 for ( int i = 0; i < images. length; ++i ) { 30 url = Log. Animator. class. get. Resource( 31 "images/deitel" + i + ". png" ); 32 images[ i ] = new Image. Icon( url ); 33 } 34 2002 Prentice Hall. 35 start. Animation(); All rights reserved.
36 } 37 38 // render one frame of the animation 39 public void paint. Component( Graphics g ) 40 { 41 super. paint. Component( g ); 42 43 // draw current animation frame 44 images[ current. Image ]. paint. Icon( this, g, 0, 0 ); 45 current. Image = ( current. Image + 1 ) % total. Images; 46 } 47 48 // start Timer that drives animation 49 public void start. Animation() 50 { 51 // if animation. Timer is null, restart animation 52 if ( animation. Timer == null ) { 53 current. Image = 0; 54 animation. Timer = new Timer( 50, this ); 55 animation. Timer. start(); 56 } 57 58 else // continue from last image displayed 59 60 if ( !animation. Timer. is. Running() ) 61 animation. Timer. restart(); 62 } 63 64 // repaint when Timer event occurs 65 public void action. Performed( Action. Event action. Event ) 66 { 67 repaint(); 68 } 69 Outline Display next frame of animation Fig. 6. 34 Definition of class Logo. Animator (part 2). Create Timer object to invoke 44 -45 method Lines action. Performed every 50 milliseconds Lines 52 -56 Lines 65 -68 Repaint screen every 50 milliseconds 2002 Prentice Hall. All rights reserved.
70 // stop Timer that drives animation 71 public void stop. Animation() 72 { 73 animation. Timer. stop(); 74 } 75 76 // get animation preferred width and height 77 public Dimension get. Preferred. Size() 78 { 79 return new Dimension( 160, 80 ); 80 } 81 82 // get animation minimum width and height 83 public Dimension get. Minimum. Size() 84 { 85 return get. Preferred. Size(); 86 } 87 88 // execute bean as standalone application 89 public static void main( String args[] ) 90 { 91 // create new Logo. Animator 92 Logo. Animator animation = new Logo. Animator(); 93 94 // create new JFrame with title "Animation test" 95 JFrame application = new JFrame( "Animator test" ); 96 application. set. Default. Close. Operation ( 97 JFrame. EXIT_ON_CLOSE ); 98 99 // add Logo. Animator to JFrame 100 application. get. Content. Pane(). add( animation, 101 Border. Layout. CENTER ); 102 Outline The Layout. Manager calls these two methods to determine how to size Logo. Animator Fig. 6. 34 Definition of class Logo. Animator (part 3). Lines 77 -86 2002 Prentice Hall. All rights reserved.
103 // set the window size and validate layout 104 application. pack(); 105 application. set. Visible( true ); 106 } 107 108 } // end class Logo. Animator Outline Fig. 6. 34 Definition of class Logo. Animator (part 4). 2002 Prentice Hall. All rights reserved.
6. 3 Preparing a Class to be a Java. Bean (cont. ) Fig. 6. 35 Compile option in the Source Editor menu. 2002 Prentice Hall. All rights reserved.
6. 4 Creating a Java. Bean: Java Archive Files • Java Archive Files – Also known as JAR files – Contain Java. Beans – Contains a manifest file • Describes JAR-file contents • MANIFEST. MF – Placed in the META-INF directory in JAR file 2002 Prentice Hall. All rights reserved.
Outline 1 Main-Class: com. deitel. advjhtp 1. beans. Logo. Animator 2 3 Name: com/deitel/advjhtp 1/beans/Logo. Animator. class 4 Java-Bean: True Class Logo. Animator contains method main, so virtual machines can execute the application in this JAR file directly Specify that Logo. Animator should be treated as a Java. Bean Fig. 6. 36 Method file manifest. tmp for the Logo-Animator bean. Line 1 Line 4 2002 Prentice Hall. All rights reserved.
6. 4 Creating a Java. Bean: Java Archive Files (cont. ) Fig. 6. 37 Add images directory to Logo. Animator. jar. 2002 Prentice Hall. All rights reserved.
6. 4 Creating a Java. Bean: Java Archive Files (cont. ) Fig. 6. 38 Manifest tab of JAR Packager dialog. 2002 Prentice Hall. All rights reserved.
6. 5 Java. Bean Properties • Property – Specific attribute of a Java. Bean – Read/Write property • Defined as a set/get method pair of the form public void set. Property. Name( Data. Type value ) public Data. Type get. Property. Name() • Builder tool inspects bean methods for these pairs – Process known as introspection – Builder exposes that pair of methods as a property • e. g. , to expose the background property: public void set. Background( Color color ) public Color get. Background() 2002 Prentice Hall. All rights reserved.
1 // Fig. 6. 39: Logo. Animator 2. java 2 // Logo. Animator 2 extends Logo. Animator to include 3 // animation. Delay property and implements Color. Listener 4 package com. deitel. advjhtp 1. beans; 5 6 // Java core packages 7 import java. awt. *; 8 import java. awt. event. *; 9 10 // Java extension packages 11 import javax. swing. *; 12 13 public class Logo. Animator 2 extends Logo. Animator { 14 15 // set animation. Delay property 16 public void set. Animation. Delay( int delay ) 17 { 18 animation. Timer. set. Delay( delay ); 19 } 20 21 // get animation. Delay property 22 public int get. Animation. Delay() 23 { 24 return animation. Timer. get. Delay(); 25 } 26 27 // launch Logo. Animator in JFrame for testing 28 public static void main( String args[] ) 29 { 30 // create new Logo. Animator 2 31 Logo. Animator 2 animation = new Logo. Animator 2(); 32 Outline Extend Logo. Animator to enable 6. 39 of animation Logouser to modify. Fig. the rate Animator 2 with property animation. Delay (part 1). Builder tool will 13 expose Line animation. Delay property Lines 16 -25 2002 Prentice Hall. All rights reserved.
33 // create new JFrame and add Logo. Animator 2 to it 34 JFrame application = new JFrame( "Animator test" ); 35 application. get. Content. Pane(). add( animation, 36 Border. Layout. CENTER ); 37 38 application. set. Default. Close. Operation ( 39 JFrame. EXIT_ON_CLOSE ); 40 application. pack(); 41 application. set. Visible( true ); 42 } 43 44 } // end class Logo. Animator 2 Outline Fig. 6. 39 Logo. Animator 2 with property animation. Delay (part 2). 2002 Prentice Hall. All rights reserved.
6. 5 Java. Bean Properties (cont. ) Fig. 6. 40 Logo. Animator 2 bean with property animation. Delay exposed in Forte’s Component Inspector. 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties • Bound property – Java. Bean notifies other objects when property value changes • Accomplished via standard Java event-handling features – java. beans. Property. Change. Listener • Listens for java. beans. Property. Change. Events 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 41: Slider. Field. Panel. java 2 // Slider. Field. Panel provides a slider to adjust the animation 3 // speed of Logo. Animator 2. 4 package com. deitel. advjhtp 1. beans; 5 6 // Java core packages 7 import java. io. *; 8 import java. awt. *; 9 import java. awt. event. *; 10 import java. beans. *; 11 12 // Java extension packages When the JSlider value 13 import javax. swing. *; 14 import javax. swing. event. *; Fig. 6. 41 Definition changes, the JText. Field value 15 for class Slideris updated automatically with the 16 public class Slider. Field. Panel extends JPanel Field. Panel 17 implements Serializable { new value (and vice versa) 18 (part 1). 19 private JSlider slider; 20 private JText. Field field; Lines 19 -20 21 private Box box. Container; 22 private int current. Value; 23 Lines 25 -31 Property. Change. Support 24 // object to support bound property changes 25 private Property. Change. Support change. Support ; provides the listener registration 26 and notification services 27 // Slider. Field. Panel constructor 28 public Slider. Field. Panel() 29 { 30 // create Property. Change. Support for bound properties 31 change. Support = new Property. Change. Support( this ); 32 2002 Prentice Hall. All rights reserved.
Outline 33 // initialize slider and text field 34 slider = new JSlider( 35 Swing. Constants. HORIZONTAL, 1, 100, 1 ); 36 field = new JText. Field( 37 String. value. Of( slider. get. Value() ), 5 ); 38 39 // set box layout and add slider and text field 40 box. Container = new Box( Box. Layout. X_AXIS ); 41 box. Container. add( slider ); 42 box. Container. add( Box. create. Horizontal. Strut( 5 ) ); 43 box. Container. add( field ); 44 45 set. Layout( new Border. Layout() ); 46 add( box. Container ); Fig. 6. 41 Definition 47 for class Slider 48 // add Change. Listener for JSlider Register the Change. Listener Field. Panel 49 slider. add. Change. Listener( for JSlider 50 (part 2). 51 new Change. Listener() { 52 Line 49 53 // handle state change for JSlider 54 public void state. Changed( Change. Event change. Event ) 55 { Line 56 56 set. Current. Value ( slider. get. Value() ); 57 } 58 59 } // end anonymous inner class When JSlider value changes, call 60 set. Current. Value to update the 61 ); // end call to add. Change. Listener 62 JText. Field and notify registered Property. Change. Listeners of change 2002 Prentice Hall. All rights reserved.
Outline 63 // add Action. Listener for JText. Field Register the Change. Listener 64 field. add. Action. Listener( for JText. Field 65 66 new Action. Listener() { 67 68 // handle action for JText. Field 69 public void action. Performed( Action. Event 70 action. Event ) 71 { 72 set. Current. Value ( 73 Integer. parse. Int( field. get. Text() ) ); 74 } Fig. 6. 41 Definition 75 When JSlider value changes, call for class Slider 76 } // end anonymous inner class set. Current. Value to update the 77 Field. Panel JSlider and notify registered 78 ); // end call to add. Action. Listener (part 3). 79 Property. Change. Listeners of change 80 } // end Slider. Field. Panel constructor 81 Line 64 82 // add Property. Change. Listener 83 public void add. Property. Change. Listener ( 84 Property. Change. Listener listener ) Line to 69 support the Methods 85 { registration of Property 86 change. Support. add. Property. Change. Listener ( listener ); Change. Listeners Lines 83 -94 87 } 88 89 // remove Property. Change. Listener 90 public void remove. Property. Change. Listener ( 91 Property. Change. Listener listener ) 92 { 93 change. Support. remove. Property. Change. Listener ( listener ); 94 } 95 2002 Prentice Hall. All rights reserved.
Outline 96 // set minimum. Value property 97 public void set. Minimum. Value( int minimum ) 98 { Builder tool will expose 99 slider. set. Minimum( minimum ); minimum. Value as a property 100 101 if ( slider. get. Value() < slider. get. Minimum() ) { 102 slider. set. Value( slider. get. Minimum() ); 103 field. set. Text( String. value. Of( slider. get. Value() ) ); 104 } 105 } 106 107 // get minimum. Value property 108 public int get. Minimum. Value() 109 { Fig. 6. 41 Definition 110 return slider. get. Minimum(); for class Slider 111 } Field. Panel 112 113 // set maximum. Value property (part 4). 114 public void set. Maximum. Value( int maximum ) 115 { Builder tool will 97 -111 expose Lines 116 slider. set. Maximum( maximum ); maximum. Value as a property 117 118 if ( slider. get. Value() > slider. get. Maximum() ) { Lines 114 -128 119 slider. set. Value( slider. get. Maximum() ); 120 field. set. Text( String. value. Of( slider. get. Value() ) ); 121 } 122 } 123 124 // get maximum. Value property 125 public int get. Maximum. Value() 126 { 127 return slider. get. Maximum(); 128 } 129 2002 Prentice Hall. All rights reserved.
Outline 130 // set current. Value property 131 public void set. Current. Value( int current ) 132 throws Illegal. Argument. Exception 133 { 134 if ( current < 0 ) 135 throw new Illegal. Argument. Exception (); 136 137 int old. Value = current. Value; 138 139 // set current. Value property 140 current. Value = current; 141 Fig. 6. 41 Definition 142 // change slider and textfield values for class Slider 143 slider. set. Value( current. Value ); 144 field. set. Text( String. value. Of( current. Value ) ); Field. Panel 145 (partwill 5). expose 146 // fire Property. Change Builder tool 147 change. Support. fire. Property. Change( current. Value as a property 148 "current. Value", new Integer( old. Value ), Lines 131 -156 149 new Integer( current. Value ) ); 150 } current. Value is. Lines a bound property, 151 147 -149 because it notifies registered Property 152 // get current. Value property 153 public int get. Current. Value() Change. Listeners change (via Lineof 159 154 { method fire. Property. Change) 155 return slider. get. Value(); 156 } 157 158 // set field. Width property Builder tool will expose 159 public void set. Field. Width( int columns ) field. Width as a property 160 { 161 field. set. Columns( columns ); 162 box. Container. validate(); 163 } 2002 Prentice Hall. 164 All rights reserved.
165 // get field. Width property 166 public int get. Field. Width() 167 { 168 return field. get. Columns(); 169 } 170 171 // get minimum panel size 172 public Dimension get. Minimum. Size() 173 { 174 return box. Container. get. Minimum. Size(); 175 } 176 177 // get preferred panel size 178 public Dimension get. Preferred. Size() 179 { 180 return box. Container. get. Preferred. Size(); 181 } 182 183 } // end class Slider. Field. Panel Builder tool will expose field. Width as a property Outline Fig. 6. 41 Definition for class Slider. Field. Panel (part 6). Line 166 2002 Prentice Hall. All rights reserved.
Outline 1 2 Name: com/deitel/advjhtp 1/beans/Slider. Field. Panel. class 3 Java-Bean: True Specify that Slider. Field. Panel should be treated as a Java. Bean Fig. 6. 42 Manifest file for the Slider. Field. Panel Java. Bean. Line 3 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties (cont. ) Fig. 6. 43 Change properties current. Value and maximum. Value. 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties (cont. ) Fig. 6. 44 Select property. Change event. 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties (cont. ) Fig. 6. 45 Select animation. Delay property of Logo. Animator 2. 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties (cont. ) Fig. 6. 46 Select current. Value Bound Property. 2002 Prentice Hall. All rights reserved.
6. 6 Bound Properties (cont. ) Fig. 6. 47 JFrame with Logo. Animator 2 and Slider. Field. Panel. 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events • Indexed property – Similar to standard property – Array of primitives of objects public void set. Property. Name( Data. Type[] data ) public void set. Property. Name( int index, Data. Type data ) public Data. Type[] get. Property. Name() public Data. Type get. Property. Name( int index ) 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events (cont. ) • Custom event – Provides functionality not provided by standard Java events – Extends class java. util. Event. Object – Listener interface extends java. util. Event. Listener 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 48 Color. Event. java 2 // Color. Event is an Event. Object subclass that indicates a 3 // change in color. 4 package com. deitel. advjhtp 1. beans; 5 6 // Java core packages 7 import java. util. *; 8 import java. awt. Color; 9 10 public class Color. Event extends Event. Object { 11 Represents the value of 12 private Color color; Color. Event’s color property 13 Fig. 6. 48 14 // constructor sets color property Color. Event 15 public Color. Event( Object source, Color color ) 16 { custom-event class 17 super( source ); indicating a color 18 set. Color( color ); change. 19 } 20 21 // set method for color property Line 12 22 public void set. Color( Color new. Color ) 23 { 24 color = new. Color; Lines 22 -31 25 } 26 27 // get method for color property Builder tool will expose 28 public Color get. Color() color as a property 29 { 30 return color; 31 } 32 33 } // end class Color. Event 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 49 Color. Listener. java 2 // Color listener is the interface for custom event Color. Event. 3 package com. deitel. advjhtp 1. beans; 4 5 // Java core packages 6 import java. util. *; 7 8 public interface Color. Listener extends Event. Listener { Custom listener interface that 9 calls registered listeners’ 10 // send color. Changed Color. Event to listener color. Changed method 11 public void color. Changed( Color. Event color. Event ); 12 with a Color. Event 13 } // end interface Color. Listener describing the change Fig. 6. 48 Color. Listener interface for receiving color. Changed notifications. Lines 8 -11 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 50 Color. Slider. Panel. java 2 // Color. Slider. Panel contains 3 Slider. Field. Panels connected to 3 // indexed property red. Green. Blue that adjusts the red, green 4 // and blue colors of an object. 5 package com. deitel. advjhtp 1. beans; 6 Color. Slider. Panel is a Java. Bean that 7 // Java core packages issues color. Changed Color. Events 8 import java. io. *; 9 import java. awt. *; when sliders change color value 10 import java. awt. event. *; 11 import java. beans. *; 12 import java. util. *; 13 14 // Java extension packages Fig. 6. 50 Definition 15 import javax. swing. *; of class 16 import javax. swing. event. *; Color. Slider. Panel 17 18 public class Color. Slider. Panel extends JPanel (part 1). 19 implements Serializable { 20 Line 18 21 private JLabel red. Label, green. Label, blue. Label; 22 private Slider. Field. Panel red. Slider , green. Slider, blue. Slider; 23 private JPanel label. Panel, slider. Panel; Lines 24 -34 24 private int[] red. Green. Blue; 25 public int RED_INDEX = 0; 26 public int GREEN_INDEX = 1; 27 public int BLUE_INDEX = 2; Initialize the indexed 28 private Set listeners = new Hash. Set(); property red. Green. Blue 29 30 // constructor for Color. Slider. Panel 31 public Color. Slider. Panel() 32 { 33 // initialize red. Green. Blue property 34 red. Green. Blue = new int[] { 0, 0, 0 }; 2002 Prentice Hall. 35 All rights reserved.
36 // initialize gui components for red slider 37 red. Label = new JLabel( "Red: " ); 38 red. Slider = new Slider. Field. Panel(); 39 red. Slider. set. Minimum. Value( 0 ); 40 red. Slider. set. Maximum. Value( 255 ); 41 42 // initialize gui components for green slider 43 green. Label = new JLabel( "Green: " ); 44 green. Slider = new Slider. Field. Panel(); 45 green. Slider. set. Minimum. Value( 0 ); 46 green. Slider. set. Maximum. Value( 255 ); 47 48 // initialize gui components for blue slider 49 blue. Label = new JLabel( "Blue: " ); 50 blue. Slider = new Slider. Field. Panel(); 51 blue. Slider. set. Minimum. Value( 0 ); 52 blue. Slider. set. Maximum. Value( 255 ); 53 54 // set layout and add components 55 set. Layout( new Border. Layout() ); 56 57 label. Panel = new JPanel( new Grid. Layout( 3, 1 ) ); 58 label. Panel. add( red. Label ); 59 label. Panel. add( green. Label ); 60 label. Panel. add( blue. Label ); 61 62 slider. Panel = new JPanel( new Grid. Layout( 3, 1 ) ); 63 slider. Panel. add( red. Slider ); 64 slider. Panel. add( green. Slider ); 65 slider. Panel. add( blue. Slider ); 66 67 add( label. Panel, Border. Layout. WEST ); 68 add( slider. Panel, Border. Layout. CENTER ); 69 Outline Initialize the red, green and blue Slider. Field. Panels Fig. 6. 50 Definition of class Color. Slider. Panel (part 2). Lines 37 -52 2002 Prentice Hall. All rights reserved.
70 // add Property. Change. Listener for red. Slider 71 red. Slider. add. Property. Change. Listener ( 72 73 new Property. Change. Listener() { 74 75 // handle property. Change for red. Slider 76 public void property. Change( Property. Change. Event 77 property. Change. Event ) 78 { 79 set. Red. Green. Blue ( RED_INDEX, 80 red. Slider. get. Current. Value() ); 81 } 82 83 } // end anonymous inner class 84 85 ); // end call to add. Property. Change. Listener 86 87 // add Property. Change. Listener for green. Slider 88 green. Slider. add. Property. Change. Listener ( 89 90 new Property. Change. Listener() { 91 92 // handle property. Change for green. Slider 93 public void property. Change( Property. Change. Event 94 property. Change. Event ) 95 { 96 set. Red. Green. Blue ( GREEN_INDEX, 97 green. Slider. get. Current. Value() ); 98 } 99 100 } // end anonymous inner class 101 102 ); // end call to add. Property. Change. Listener 103 Outline When user changes red. Slider value, red. Slider sets indexed property red. Green. Blue Fig. 6. 50 Definition of class Color. Slider. Panel (part 3). Lines 71 -85 When user changes green. Slider value, Lines 88 -102 green. Slider sets indexed property red. Green. Blue 2002 Prentice Hall. All rights reserved.
104 // add Property. Change. Listener for blue. Slider 105 blue. Slider. add. Property. Change. Listener ( 106 107 new Property. Change. Listener() { 108 109 // handle property. Change for blue. Slider 110 public void property. Change( Property. Change. Event 111 property. Change. Event ) 112 { 113 set. Red. Green. Blue ( BLUE_INDEX, 114 blue. Slider. get. Current. Value() ); 115 } 116 117 } // end anonymous inner class 118 119 ); // end call to add. Property. Change. Listener 120 121 } // end Color. Slider. Panel constructor 122 123 // add Color. Listener 124 public void add. Color. Listener( 125 Color. Listener color. Listener ) 126 { 127 // listeners must be accessed atomically 128 synchronized ( listeners ) { 129 listeners. add( color. Listener ); 130 } 131 } 132 Outline When user changes blue. Slider value, blue. Slider sets indexed property red. Green. Blue Fig. 6. 50 Definition of class Color. Slider. Panel (part 4). Lines 105 -121 2002 Prentice Hall. All rights reserved.
Outline 133 // remove Color. Listener 134 public void remove. Color. Listener( 135 Color. Listener color. Listener ) 136 { 137 // listeners must be accessed by one thread only 138 synchronized ( listeners ) { 139 listeners. remove( color. Listener ); 140 } 141 } 142 Invoked when user changes 143 // fire Color. Event red. Green. Blue property 144 public void fire. Color. Changed() Fig. 6. 50 Definition 145 { 146 Iterator iterator; of class 147 Color. Slider. Panel 148 // listeners must be accessed atomically (part 5). 149 synchronized ( listeners ) { 150 iterator = new Hash. Set( listeners ). iterator(); 151 } Line 144 to send Create Color. Event 152 to all Color. Listeners 153 // create new Color with values of red. Green. Blue 154 // create new Color. Event with color variable Lines 155 -158 155 Color color = new Color( red. Green. Blue[ RED_INDEX ], 156 red. Green. Blue[ GREEN_INDEX ], Lines 161 -165 157 red. Green. Blue [ BLUE_INDEX ] ); 158 Color. Event color. Event = new Color. Event( this, color ); 159 160 // notify all registered Color. Listeners of Color. Change 161 while ( iterator. has. Next() ) { Notify all Color 162 Color. Listener color. Listener = ( Color. Listener ) Listeners of color change 163 iterator. next(); 164 color. Listener. color. Changed( color. Event ); 165 } 166 } 2002 Prentice Hall. 167 All rights reserved.
168 // get red. Green. Blue property 169 public int[] get. Red. Green. Blue() 170 { 171 return red. Green. Blue; 172 } 173 174 // get red. Green. Blue indexed property 175 public int get. Red. Green. Blue( int index ) 176 { 177 return red. Green. Blue[ index ]; 178 } 179 180 // set red. Green. Blue property 181 public void set. Red. Green. Blue( int[] array ) 182 { 183 red. Green. Blue = array; 184 } 185 186 // set red. Green. Blue indexed property 187 public void set. Red. Green. Blue( int index, int value ) 188 { 189 red. Green. Blue[ index ] = value; 190 fire. Color. Changed(); 191 } 192 193 } // end class Color. Slider. Panel Outline Builder tool will expose red. Green. Blue as a indexed property Fig. 6. 50 Definition of class Color. Slider. Panel (part 6). Lines 169 -191 2002 Prentice Hall. All rights reserved.
Outline 1 2 Name: com/deitel/advjhtp 1/beans/Color. Slider. Panel. class 3 Java-Bean: True Specify that Color. Slider. Panel should be treated as a Java. Bean Fig. 6. 51 Manifest file for the Color. Slider. Panel Java. Bean. Line 3 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events (cont. ) Fig. 6. 52 Selecting color. Changed method in Connection Wizard 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events (cont. ) Fig. 6. 53 Selecting set. Background method for target Logo. Animator 2. 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events (cont. ) Fig. 6. 54 Entering user code in Connection Wizard. 2002 Prentice Hall. All rights reserved.
6. 7 Indexed Properties and Custom Events (cont. ) Fig. 6. 55 Using the Color. Slider. Panel to change the background color of Logo. Animator 2. 2002 Prentice Hall. All rights reserved.
6. 8 Customizing Java. Beans for Builder Tools • Customizing exposed sets of properties – Supply class that implements interface Bean. Info • Package java. beans provides class Simple. Bean. Info • Describes to builder tool how to present customized features 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 56 Slider. Field. Panel. Bean. Info. java 2 // Slider. Field. Panel. Bean. Info is the Bean. Info class for 3 // Slider. Field. Panel 4 package com. deitel. advjhtp 1. beans; 5 6 // Java core packages Slider. Field. Panel. Bean. Info 7 import java. beans. *; extends Simple. Bean. Info, which 8 import java. awt. Image; implements interface Bean. Info 9 10 // Java extension packages Fig. 6. 56 11 import javax. swing. *; Slider. Field. Panel 12 13 public class Slider. Field. Panel. Bean. Info extends Simple. Bean. Info { Bean. Info exposes 14 properties and events 15 public static final Class bean. Class = Override method get. Bean. Descriptor for Slider. Field 16 Slider. Field. Panel. class; to return Java. Bean’s Panel name and 17 of description class 18 // return general description of bean (part 1). 19 public Bean. Descriptor get. Bean. Descriptor () 20 { 21 Bean. Descriptor descriptor = new Bean. Descriptor( Line 13 22 bean. Class, Slider. Field. Panel. Customizer. class ); 23 descriptor. set. Display. Name( "Slider Field" ); 24 descriptor. set. Short. Description( Line 19 25 "A slider bar to change a numerical property. " ); 26 Line 31 27 return descriptor; 28 } 29 30 // return bean icon Override method get. Icon to 31 public Image get. Icon( int icon. Kind ) display the appropriate image 32 { 33 Image image = null; 34 2002 Prentice Hall. All rights reserved.
Outline 35 switch( icon. Kind ) { 36 37 case ICON_COLOR_16 x 16: 38 image = load. Image( "icon 1. gif" ); 39 break; 40 41 case ICON_COLOR_32 x 32: 42 image = load. Image( "icon 2. gif" ); 43 break; 44 45 case ICON_MONO_16 x 16: 46 image = load. Image( "icon 3. gif" ); 47 break; Fig. 6. 56 48 Slider. Field. Panel 49 case ICON_MONO_32 x 32: Bean. Info exposes 50 image = load. Image( "icon 4. gif" ); 51 break; properties and events 52 for Slider. Field 53 default: Panel of class 54 break; 55 } (part 2). 56 57 return image; 58 } Line 62 59 60 // return array of Method. Descriptors for public get methods 61 // of class Slider. Field. Panel 62 public Method. Descriptor[] get. Method. Descriptors() Each Method. Descriptor 63 { represents a specific method 64 // create array of Method. Descriptors 65 try { exposed to the builder tool 66 Method. Descriptor get. Minimum. Value = new 67 Method. Descriptor ( bean. Class. get. Method( 68 "get. Minimum. Value", null ) ); 2002 Prentice Hall. 69 All rights reserved.
Outline 70 Method. Descriptor get. Maximum. Value = new 71 Method. Descriptor ( bean. Class. get. Method( 72 "get. Maximum. Value", null ) ); Describe get methods for properties 73 maximum. Value, minimum. Value, 74 Method. Descriptor get. Current. Value = new current. Value and field. Width 75 Method. Descriptor ( bean. Class. get. Method( 76 "get. Current. Value", null ) ); 77 78 Method. Descriptor get. Field. Width = new 79 Method. Descriptor ( bean. Class. get. Method( 80 "get. Field. Width", null ) ); 81 Method. Descriptor [] descriptors = { get. Minimum. Value, 82 get. Maximum. Value , get. Current. Value, get. Field. Width }; Fig. 6. 56 83 Slider. Field. Panel 84 return descriptors; Bean. Info exposes 85 } 86 properties and events 87 // print. Stack. Trace if No. Such. Method. Exception thrown for Slider. Field 88 catch ( No. Such. Method. Exception method. Exception ) { Panel of class 89 method. Exception. print. Stack. Trace(); 90 } (part 3). 91 92 // print. Stack. Trace if Security. Exception thrown 93 catch ( Security. Exception security. Exception ) { Line 70 -82 94 security. Exception. print. Stack. Trace(); 95 } 96 97 return null; 98 } 99 2002 Prentice Hall. All rights reserved.
100 // return Property. Descriptor array 101 public Property. Descriptor[] get. Property. Descriptors() 102 throws Runtime. Exception 103 { 104 // create array of Property. Descriptors 105 try { 106 107 // field. Width property 108 Property. Descriptor field. Width = new 109 Property. Descriptor ( "field. Width", bean. Class ); 110 field. Width. set. Short. Description( 111 "Width of the text field. " ); 112 113 // current. Value property 114 Property. Descriptor current. Value = new 115 Property. Descriptor ( "current. Value", bean. Class ); 116 current. Value. set. Short. Description( 117 "Current value of slider. " ); 118 119 // maximum. Value property 120 Property. Descriptor maximum. Value = new 121 Property. Descriptor ( "maximum. Value", bean. Class ); 122 maximum. Value. set. Property. Editor. Class( 123 Maximum. Value. Editor. class ); 124 maximum. Value. set. Short. Description( 125 "Maximum value of slider. " ); 126 127 // minimum. Value property 128 Property. Descriptor minimum. Value = new 129 Property. Descriptor ( "minimum. Value", bean. Class ); 130 minimum. Value. set. Short. Description( 131 "Minimum value of slider. " ); 132 minimum. Value. set. Property. Editor. Class( 133 Minimum. Value. Editor. class ); 134 Outline Property. Descriptors represents specific properties exposed to the builder tool Fig. 6. 56 Slider. Field. Panel Properties maximum. Bean. Info exposes Value, minimum. Value, properties andand events current. Value for Slider. Fieldfield. Width will be Panel of class exposed to the builder tool (part 4). Line 101 Lines 108 -133 2002 Prentice Hall. All rights reserved.
Outline 135 // ensure Property. Change. Event occurs for this property 136 current. Value. set. Bound( true ); Specify that current 137 138 Property. Descriptor descriptors[] = { field. Width, Value is a bound property 139 current. Value , maximum. Value, minimum. Value }; 140 141 return descriptors; 142 } 143 144 // throw Runtime. Exception if Introspection. Exception Fig. 6. 56 145 // thrown Slider. Field. Panel 146 catch ( Introspection. Exception exception ) { 147 throw new Runtime. Exception( exception. get. Message() ); Bean. Info exposes 148 } properties and events 149 } Indicate thatfor the. Slider. Fieldproperty at 150 151 // get current. Value property index position 1 in Property. Panel of class 152 public int get. Default. Property. Index () Descriptor (partarray 5). is the 153 { default property 154 return 1; 155 } Line 136 156 157 // return Event. Set. Descriptors array Return 158 public Event. Set. Descriptor[] get. Event. Set. Descriptors() Line array 152 of 159 throws Runtime. Exception Event. Set. Descriptors that 160 { describe to a builder Line 158 tool the 161 // create array of Event. Set. Descriptors events supports by this bean 162 try { 163 Event. Set. Descriptor changed = new 164 Event. Set. Descriptor ( bean. Class, "property. Change", 165 java. beans. Property. Change. Listener. class, 166 "property. Change"); 167 2002 Prentice Hall. All rights reserved.
Outline 168 // set event description and name 169 changed. set. Short. Description( 170 "Property change event for current. Value. " ); 171 changed. set. Display. Name( 172 "Slider. Field. Panel value changed" ); 173 174 Event. Set. Descriptor [] descriptors = { changed }; 175 176 return descriptors; 177 } 178 179 // throw Runtime. Exception if Introspection. Exception 180 // thrown Fig. 6. 56 181 catch ( Introspection. Exception exception ) { Slider. Field. Panel 182 throw new Runtime. Exception( exception. get. Message() ); Bean. Info exposes 183 } 184 } properties and events 185 Indicate that the event at for Slider. Field 186 // get Property. Change event index position 0 in Event. Set. Panel of class 187 public int get. Default. Event. Index() Descriptor array is the 188 { (part 6). 189 return 0; default event 190 } 191 Line 187 192 } // end class Slider. Field. Panel. Bean. Info 2002 Prentice Hall. All rights reserved.
6. 8 Customizing Java. Beans for Builder Tools (cont. ) Fig. 6. 57 Properties and events exposed by Slider. Field. Panel. Bean. Info. 2002 Prentice Hall. All rights reserved.
6. 8. 1 Property. Editors • Property. Editor – Determines how a property is edited inside the editor tool – Allows for further customization 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 58 Maximum. Value. Editor. java 2 // Maximum. Value. Editor is the Property. Editor for the Edits the maximum. Value 3 // maximum. Value property of the Slider. Field. Panel bean. property of class 4 package com. deitel. advjhtp 1. beans; 5 Slider. Field. Panel 6 // Java core packages 7 import java. beans. *; 8 9 public class Maximum. Value. Editor extends Property. Editor. Support { 10 11 private Integer maximum. Value; maximum. Value stores. Maximumthe Fig. 6. 58 12 value selected. Value. Editor in the combo boxis a 13 // set maximum. Value property Property. Editor 14 public void set. Value( Object value ) 15 { for Slider. Field 16 maximum. Value = ( Integer ) value; Panel’s maximum 17 fire. Property. Change (); Set maximum. Value (part 1). 18 } and notify listeners 19 20 // get maximum. Value property Line 9 21 public Object get. Value() 22 { 23 return maximum. Value; Line 11 24 } 25 26 // set maximum. Value property from text string Lines 14 -18 27 public void set. As. Text( String string ) 28 { 29 // decode may throw Number. Format. Exception 30 try { 31 maximum. Value = Integer. decode( string ); 32 fire. Property. Change (); 33 } 34 2002 Prentice Hall. All rights reserved.
Outline 35 // throw Illegal. Argument. Exception if decode throws 36 // Number. Format. Exception 37 catch ( Number. Format. Exception number. Format. Exception ) { 38 throw new Illegal. Argument. Exception (); 39 } 40 41 } // end method set. As. Text Return array of all tags that 42 appear in combo box 43 // get String array for pull-down menu 44 public String[] get. Tags() 45 { 46 return new String[] { "50", "100", "150", "200", "250", 47 "300", "350", "400", "450", "500" }; Fig. 6. 58 Maximum 48 } 49 Value. Editor is a 50 // get maximum. Value property as string Property. Editor 51 public String get. As. Text() for Slider. Field 52 { 53 return get. Value(). to. String(); Panel’s maximum 54 } Value (part 2). 55 56 // get initialization string for Java code 57 public String get. Java. Initialization. String () Lines 44 -48 58 { 59 return get. Value(). to. String(); 60 } 61 62 } // end class Maximum. Value. Editor 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 59 Minimum. Value. Editor. java 2 // Minimum. Value. Editor is the Property. Editor for the Edits the minimum. Value 3 // minimum. Value property of the Slider. Field. Panel bean. property of class 4 package com. deitel. advjhtp 1. beans; 5 Slider. Field. Panel 6 // Java core packages 7 import java. beans. *; 8 9 public class Minimum. Value. Editor extends Property. Editor. Support { 10 11 protected Integer minimum. Value; minimum. Value the Fig. 6. 59 stores Minimum 12 Value. Editor is a value selected in the combo box 13 // set minimum. Value property Property. Editor 14 public void set. Value( Object value ) 15 { for Slider. Field 16 minimum. Value = ( Integer ) value; Panel’s minimum 17 fire. Property. Change (); Set minimum. Value (part 1). 18 } and notify listeners 19 20 // get value of property minimum Line 9 21 public Object get. Value() 22 { 23 return minimum. Value; Line 11 24 } 25 26 // set maximum. Value property from text string Lines 14 -18 27 public void set. As. Text( String string ) 28 { 29 // decode may throw Number. Format. Exception 30 try { 31 minimum. Value = Integer. decode( string ); 32 fire. Property. Change (); 33 } 34 2002 Prentice Hall. All rights reserved.
Outline 35 // throw Illegal. Argument. Exception if decode throws 36 // Number. Format. Exception 37 catch ( Number. Format. Exception number. Format. Exception ) { 38 throw new Illegal. Argument. Exception (); 39 } 40 } Return array of all tags that 41 appear in combo box 42 // string array for pull-down menu 43 public String[] get. Tags() 44 { 45 return new String[] { "50", "100", "150", "200", "250", 46 "300", "350", "400", "450", "500" }; 47 } Fig. 6. 59 Minimum 48 49 // get minimum. Value property as string Value. Editor is a 50 public String get. As. Text() Property. Editor 51 { for Slider. Field 52 return get. Value(). to. String(); 53 } Panel’s minimum 54 Value (part 2). 55 // get initialization string for Java code 56 public String get. Java. Initialization. String () 57 { Lines 43 -47 58 return get. Value(). to. String(); 59 } 60 61 } // end class Minimum. Value. Editor 2002 Prentice Hall. All rights reserved.
6. 8. 1 Property. Editors (cont. ) Fig. 6. 60 Maximum. Value. Editor and Minimum. Value. Editor pull-down menus in Forte. 2002 Prentice Hall. All rights reserved.
6. 8. 1 Property. Editors (cont. ) Fig. 6. 61 Slider. Field. Panel values constrained by Property. Editors. 2002 Prentice Hall. All rights reserved.
6. 8. 2 Customizers • Customizer – Creates customized interface for setting bean properties – Useful for manipulating beans that cannot be edited in standard property sheets 2002 Prentice Hall. All rights reserved.
Outline 1 // Fig. 6. 62 Slider. Field. Panel. Customizer. java 2 // Slider. Field. Panel. Customizer is the Customizer class for 3 // Slider. Field. Panel. 4 package com. deitel. advjhtp 1. beans; 5 6 // Java core packages 7 import java. awt. *; 8 import java. awt. event. *; Classes that implement 9 import java. beans. *; Customizer must provide a 10 import java. util. *; no-argument constructor, Fig. 6. 62 so the 11 12 // Java extension packages builder tool Slider. Fieldcan instantiate it 13 import javax. swing. *; Panel. Customizer 14 custom GUI for 15 public class Slider. Field. Panel. Customizer extends JPanel modifying Slider 16 implements Customizer { 17 Field. Panel beans 18 private JCombo. Box maximum. Combo, minimum. Combo; (part 1). 19 private JLabel minimum. Label, maximum. Label; 20 protected Slider. Field. Panel slider; 21 private Property. Change. Support change. Support ; Line 15 22 private static final String[] VALUES = { "50", "100", Array of all tags that 23 "150", "200", "250", "300", "350", "400", "450", "500" }; appear in combo box 24 Lines 22 -23 25 // initialize GUI components 26 public Slider. Field. Panel. Customizer () Line 29 27 { Property. Change 28 // create Property. Change. Support to handle Property. Change Support provides the 29 change. Support = new Property. Change. Support( this ); listener registration and 30 31 // labels for maximum and minimum properties notification services 32 minimum. Label = new JLabel( "Minimum Slider Value: " ); 33 maximum. Label = new JLabel( "Maximum Slider Value: " ); 34 2002 Prentice Hall. All rights reserved.
Outline 35 // combo boxes adjust maximum and minimum properties 36 minimum. Combo = new JCombo. Box( VALUES ); Properties minimum. Value and 37 minimum. Combo. set. Selected. Index( 0 ); 38 maximum. Combo = new JCombo. Box( VALUES ); maximum. Value can be set from 50 39 maximum. Combo. set. Selected. Index( 9 ); through 500, in increments of 50 40 41 // add Action. Listener to minimum. Value combo box 42 minimum. Combo. add. Action. Listener( When user accesses maximum. Value 43 44 new Action. Listener() { combo box, change maximum. Value Fig. 6. 62 45 Slider. Field 46 // handle action of minimum combo box 47 public void action. Performed( Action. Event event ) Panel. Customizer 48 { custom GUI for 49 set. Minimum ( minimum. Combo. get. Selected. Index() ); modifying Slider 50 } 51 Field. Panel beans 52 } // end anonymous inner class (part 2). 53 54 ); // end add. Action. Listener 55 Lines 36 -39 56 // add Action. Listener to maximum. Value combo box 57 maximum. Combo. add. Action. Listener( When user accesses minimum. Value 58 Line 42 59 new Action. Listener() { combo box, change minimum. Value 60 Line 57 61 // handle action of maximum combo box 62 public void action. Performed( Action. Event event ) 63 { 64 set. Maximum ( maximum. Combo. get. Selected. Index() ); 65 } 66 67 } // end anonymous inner class 68 2002 Prentice Hall. 69 ); // end add. Action. Listener All rights reserved.
Outline 71 add( minimum. Label ); 72 add( minimum. Combo ); 73 add( maximum. Label ); 74 add( maximum. Combo ); 75 } 76 77 // set the customized object Method set. Object takes as 78 public void set. Object( Object bean ) an argument an instance of the 79 { Java. Bean being instantiaed 80 slider = ( Slider. Field. Panel ) bean; 81 } 82 83 // add Property. Change. Listener with Property. Change. Support Fig. 6. 62 84 public void add. Property. Change. Listener ( Slider. Field 85 Property. Change. Listener listener ) Panel. Customizer 86 { 87 change. Support. add. Property. Change. Listener ( listener ); custom GUI for 88 } modifying Slider 89 Field. Panel beans 90 // remove Property. Change. Listener with Property. Change. Support 91 public void remove. Property. Change. Listener ( (part 3). 92 Property. Change. Listener listener ) 93 { 94 change. Support. remove. Property. Change. Listener ( listener ); Lines 78 -81 95 } 96 97 // set minimum. Value property 98 public void set. Minimum( int index ) 99 { 100 int old. Value = slider. get. Minimum. Value(); 101 int new. Value = Integer. parse. Int( VALUES[ index ] ); 102 2002 Prentice Hall. All rights reserved.
103 slider. set. Minimum. Value( new. Value ); 104 change. Support. fire. Property. Change( "minimum. Value", 105 new Integer( old. Value ), new Integer( new. Value ) ); 106 } 107 108 // set maximum. Value property 109 public void set. Maximum( int index ) 110 { 111 int old. Value = slider. get. Maximum. Value(); 112 int new. Value = Integer. parse. Int( VALUES[ index ] ); 113 114 slider. set. Maximum. Value( new. Value ); 115 change. Support. fire. Property. Change( "maximum. Value", 116 new Integer( old. Value ), new Integer( new. Value ) ); 117 } 118 119 } // end class Slider. Field. Panel. Customizer Notify all Property. Outline Change. Listeners of change in minimum. Value Notify. Fig. all Property 6. 62 Change. Listeners of Slider. Fieldchange in maximum. Value Panel. Customizer custom GUI for modifying Slider. Field. Panel beans (part 4). Lines 104 -105 Lines 115 -116 2002 Prentice Hall. All rights reserved.
6. 8. 2 Customizers (cont. ) Fig. 6. 63 Select Customize from Component Inspector menu. 2002 Prentice Hall. All rights reserved.
6. 8. 2 Customizers (cont. ) Fig. 6. 64 Slider. Field. Panel’s Customizer Dialog. 2002 Prentice Hall. All rights reserved.
- Slides: 100