First Bean - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

First Bean

Description:

All beans must implement Serializable Interface. Sets background color and ... Uses either design pattern introspection or BeanInfo class to discover events ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 25
Provided by: russell5
Category:

less

Transcript and Presenter's Notes

Title: First Bean


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 beans appearance
    and behavior that can be changed at design time.
  • Private values accessed through getter and setter
    methods
  • Encapsulation, Design Pattern
  • Can be inherited (Canvas for SimpleBean)

9
Builder Tools and Properties
  • Discover Beans 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 addltEventListenerTypegt(ltEventListenerT
ypegt a) public void removeltEventListenerTypegt(ltEve
ntListenerTypegt 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 implementPropertyCha
    ngeListener interface
  • Beanbox handles events with eventhookup adapter
  • Tutorial

15
  • Import java.beans package
  • Instantiate a PropertyChangeSupport object
  • Implement methods to maintain list of property
    change event listeners
  • Modify a property's setter method to fire a
    property change event when the property is
    changed
  • Listeners implement PropertyChangeListener
    interface withpropertyChange (PropertyChangeEvent
    ) method

16
1.Drop OurButton and ChangeReporter instances on
the BeanBox. 2.Select the OurButton instance and
choose the EditEventspropertyChangepropertyCha
nge 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
package sunw.demo.misc import
java.awt. import java.beans. import
java.io.Serializable / A simple extension
of TextField that handles PropertyChangeEvents.
This Bean handles PropertyChange events by
displaying the name of the property and the
new property value. A Java Bean that displays
bound properties can be connected to a
ChangeReporter with the BeanBox. To do so
programatically is straightforward ltpregt
import sunw.demo.misc.OurButton import
sunw.demo.misc.ChangeReporter import
java.awt. import java.beans.
18
public class DemoChangeReporter
OurButton button new OurButton()
ChangeReporter reporter new ChangeReporter()
PropertyChangeAdapter adapter new
PropertyChangeAdapter()
DemoChangeReporter()
button.addPropertyChangeListener(adapter)
button.setLabel("Report This") Frame
f new Frame("Demo Change Reporter")
f.setLayout(new FlowLayout())
f.add(button) f.add(reporter)
f.pack() f.show()
class PropertyChangeAdapter implements
PropertyChangeListener public void
propertyChange(PropertyChangeEvent e)
reporter.reportChange(e)

19
public static void main(String argv)
new DemoChangeReporter()
lt/pregt In the example above, the button is
connected to the ChangeReporter with a
(nested) adpater class. When the
DemoChangeReporter object is constructed the
buttons label field is set, and the adapters
propertyChange method runs. / public
class ChangeReporter extends TextField implements
Serializable public ChangeReporter()
super("", 35) setEditable(false)
public void reportChange(PropertyChangeEvent evt)
String text evt.getPropertyName() " "
evt.getNewValue() int width getSize().width
- 10 Font f getFont() if (f ! null)
// Trim the text to fit. FontMetrics fm
getFontMetrics(f) while (fm.stringWidth(text)
gt width) text text.substring(0,
text.length()-1)
setText(text)
20
package sunw.demo.buttons import
java.awt. import java.awt.event. import
java.beans. import java.io.Serializable import
java.util.Vector / A simple Java Beans
button. OurButton is a "from-scratch"
lightweight AWT component. It's a good example
of how to implement bound properties and
support for event listeners. Parts of the
source are derived from sun.awt.tiny.TinyButtonPee
r. / public class OurButton extends Component
implements Serializable, MouseListener,
MouseMotionListener / Constructs
a Button with the a default label. /
public OurButton() this("press")
21
//------------------------------------------------
---------------------- // Methods for
registering/deregistering event listeners
/ The specified ActionListeners
ltbgtactionPerformedlt/bgt method will be
called each time the button is clicked. The
ActionListener object is added to a list
of ActionListeners managed by this
button, it can be removed with removeActionListene
r. Note the JavaBeans specification does
not require ActionListeners to run in any
particular order. _at_see
removeActionListener _at_param l the
ActionListener / public
synchronized void addActionListener(ActionListener
l) pushListeners.addElement(l)
/ Remove this ActionListener from the
buttons internal list. If the
ActionListener isn't on the list, silently do
nothing. _at_see addActionListener
_at_param l the ActionListener /
public synchronized void removeActionListener(Act
ionListener l) pushListeners.removeElement(l)

22
/ The specified PropertyChangeListeners
ltbgtpropertyChangelt/bgt method will be
called each time the value of any bound property
is changed. The PropertyListener object is
addded to a list of PropertyChangeListeners
managed by this button, it can be removed with
removePropertyChangeListener. Note the
JavaBeans specification does not require
PropertyChangeListeners to run in any
particular order. _at_see
removePropertyChangeListener _at_param l the
PropertyChangeListener / public
void addPropertyChangeListener(PropertyChangeListe
ner l) changes.addPropertyChangeListener(l)
/ Remove this
PropertyChangeListener from the buttons internal
list. If the PropertyChangeListener
isn't on the list, silently do nothing.
_at_see addPropertyChangeListener
_at_param l the PropertyChangeListener /
public void removePropertyChangeListener(Prop
ertyChangeListener l) changes.removePropertyCha
ngeListener(l)
23
//------------------------------------------------
---------------------- / This
method has the same effect as pressing the
button. _at_see addActionListener
/ public void fireAction() if
(debug) System.err.println("Button "
getLabel() " pressed.") Vector
targets synchronized (this) targets
(Vector) pushListeners.clone() ActionEvent
actionEvt new ActionEvent(this, 0, null) for
(int i 0 i lt targets.size() i)
ActionListener target (ActionListener)targets.el
ementAt(i) target.actionPerformed(actionEvt)
private PropertyChangeSupport
changes new PropertyChangeSupport(this)
private Vector pushListeners new Vector()
24
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 information
Write a Comment
User Comments (0)
About PowerShow.com