Download presentation
Presentation is loading. Please wait.
Published byClare Sullivan Modified over 9 years ago
1
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean
2
import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ //Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); }
3
All beans must implement Serializable Interface Sets background color and size Compile the Bean Create manifest file Create Jar file Load Jar in BeanBox Drop SimpleBean instance in BeanBox
4
Manifest File Describes entries in jar archive Made up of attribute/value pairs Format is “attribute: value” Name: SimpleBean.class Java-Bean: True
5
JAR Java archive file format Bundle any number of files ZIP compression Manifest describes content jar part of the jdk jar {ctx} [vfm0M] [jar-file] [manifest-file] files … jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
6
Jar Command Options
7
Additional Jar Capabilities Security with Digital Signatures Decreased Applet Download Time Packaging of extensions Portability Package Sealing and Versioning Jar tutorial
8
Properties Properties are attributes of bean’s appearance and behavior that can be changed at design time. Private values accessed through getter and setter methods Encapsulation, Design Pattern
9
Builder Tools and Properties Discover Bean’s Properties Determine properties’ read/write attributes Determine property types Locate property editors Display property sheet Alter properties
10
package sunw.demo.simple; import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ private Color color = Color.green; //property getter method public Color getColor(){ return color; } //property setter method. Sets new SimpleBean //color and repaints. public void setColor(Color newColor){ color = newColor; repaint(); } public void paint(Graphics g) { g.setColor(color); g.fillRect(20, 5, 20, 30); } //Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); }
11
Events in the BeanBox Uses either design pattern introspection or BeanInfo class to discover events For a bean to be a source for an event, it must implement methods that add or remove listeners for that event For example, if a source Bean registers ActionListener objects, it will fire events at those objects by calling the actionPerformed method on those listeners. public void add ( a) public void remove ( a)
12
Bound Properties When property changes, other objects may need to be notified and react. When bound property changes, notification is sent to interested listeners. Bean with bound property must maintain list of property listeners and fire PropertyChangeEvent objects PropertyChangeSupport
13
A Bean with bound property maintains list of property change listeners Alerts those listeners when bound property changes. Convenience class PropertyChangeSupport implements methods that add and remove PropertyChangeListener objects, and fires PropertyChangeEvent objects at those listeners when the bound property changes. Beans can inherit from this class, or use it as an inner class. By implementing the PropertyChangeListener interface the listener can be added to the list maintained by the bound property Bean, and because it implements the PropertyChangeListener.propertyChange method, the listener can respond to property change notifications. The PropertyChangeEvent class encapsulates property change information, and is sent from the property change event source to each object in the property change listener list via the propertyChange method.
14
Inherit or use inner class Objects interested in event implement PropertyChangeListener interface Beanbox handles events with event hookup adapter Tutorial
15
Import java.beans package Instantiate a PropertyChangeSupport object Implement methods to maintain list of propert change event listeners Modify a property's setter method to fire a property change event when the property is changed Listeners implement PropertyChangeListener interface with propertyChange (PropertyChangeEvent) method
16
1.Drop OurButton and ChangeReporter instances on the BeanBox. 2.Select the OurButton instance and choose the Edit|Events|propertyChange|propertyChange menu item. 3.Connect the rubber band line to the ChangeReporter instance. The EventTargetDialog will be displayed. 4.Choose reportChange from the EventTargetDialog. The event hookup adapter source will be generated and compiled 5.Select OurButton and change some of it's properties. You will see change reports in ChangeReporter.
17
Other Properties Constrained Properties: Change to property can be vetoed by other objects. Indexed Properties: Properties with multiple values (e.g. like a list of choices) See Tutorial for additional informationTutorial
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.