Container Container
JavaBean1
JavaBean
JavaBean2
2
Concepts in Component Model
JavaBeans provides a platform neutral component architecture
The features of JavaBeans are
• Component fields or properties
• Component methods or functions
• Events and intercommunication
• State persistence and storage
If the properties of one component match that of another then they are
in the same state
Properties not only define the appearance but also the behavior of
components
A component may not have any appearance at all but can have behavior
eg. A component in a spreadsheet that calculates the interest earned on
some earning column but not capable of displaying the data
OutputText java.lang.String
Property Name Java Type
BGColor java.awt.Color
InputText java.lang.String
TextFont java.awt.Font
FontColor java.awt.Color Width int
TextDisplayer Bean Properties TextReader Bean Properties
4
Specifying the Events the Bean Generates or Responds To
• The TextDisplayer Bean must respond to an event specifying that its
text should change
• The TextDisplayer must update its OutputText property and redraw itself
• The TextReader Bean doesn’t need to respond to any event but should
generate an event when the user changes its InputText property
• This type of event is called a PropertyChangeEvent
they are instantiated and they use standard PropertyEditors for their
properties
TextOutput “TextDisplayer”
BGColor java.awt.Color.white
TextFont Courier, normal, 12
FontColor java.awt.Color.black
Default Property Values for the TextDisplayer Bean
Width 40
Default Property Values for the TextReader Bean
// default constructor for this Bean. This is the constructor that an application
// builder would use.
public TextDisplayer() {
application builder
// would use.
public TextReader() {
this( "", 40 ); }
this.InputText = InputText;
this.Width = Width;
setEditable( true );
}
6
}
Creating and Using Properties
•
• The TextDisplayer Bean display in black fonts in white background as the
property is set like that and the FontColor property can be changed
according to the users wish.
• If the property of a component cannot be changed by other Beans the
usefulness and reusability of the Bean is reduced.
eg. Bean’s FontColor property to red to indicate a negative value
• Four types of properties are associated with a JavaBean
• Single value property – is a property for which there is only one value
• Indexed property – an indexed property has several values each of which
has a unique index
• Bound property – a property is bound means that some other Bean is
dependent on that property. eg. TextDisplayer and TextReader
• Constrained property – a property is constrained if it must check with
other components before it can change and constrained properties
cannot change arbitrarily
return( OutputText ); }
OutputText = text;
resizeCanvas(); }
public synchronized Color getBGColor() {
return( BGColor ); }
BGColor = color;
repaint(); }
return( TextFont ); }
TextFont = font;
resizeCanvas(); }
return( FontColor ); }
8
public synchronized void setFontColor( Color color ) {
FontColor = color;
repaint(); }
Indexed Properties
Bound Properties
• The programmer can decide which all Bean properties other components
can bind to
• To provide bound properties in a Bean the following methods must be defined
public void addPropertyChangeListener( PropertyChangeListener 1 );
public void removePropertyChangeListener( PropertyChangeListener 1 );
To provide this functionality on a per-property basis the following design pattern should be used.
public void add<PropertyName>Listener( PropertyChangeListener 1 );
public void remove<PropertyName>Listener( PropertyChangeListener 1 );
•
• Beans which want to bind to other components properties should
implement the PropertyChangeListener interface which consists of
the following method
public void propertyChange( PropertyChangeEvent evt );
•
• Whenever a bound property in a Bean is updated it must call the
propertyChange() method in all the components that have registered
with it
InputText = newText;
setText( InputText );
changeAgent.addPropertyChangeListener( l ); }
PropertyChangeSupport( this );
Constrained Properties
throws PropertyVetoException {
setColumns( Width );
Component p = getParent();
vetoAgent.addVetoableChangeListener( l ); }
vetoAgent.removeVetoableChangeListener( l ); }
VetoableChangeSupport( this );
• To make the Width property constrained but not bound is to remove the line
Multicast Events
Unicast Events
pattern analysis on the areas of the Bean for which the BeanInfo
• The Introspector class has a set procedure for the way it looks for
2. If a BeanInfo class is present each method in the BeanInfo class is called to find out
whether it can provide any information.
The Introspector class will use implicit analysis to expose
information for which the BeanInfo class denies any knowledge
14
Introspector class will use implicit analysis for all the methods in
( if one exists) and starts the process over for only those
import java.beans.*;
try {
} catch( IntrospectionException e ) {
return( properties ); }
}
• The getPropertyDescriptors() method is overriden in the program
• The parent class returns null for all the other methods in the
java.beans.BeanInfo interface.
Customization Editor
• All application builders have to implement some method of
customizing the Beans
import java.beans.*;
Customizer {
public TextReaderCustomizer() {
component = (TextReader)target;
}
public boolean handleEvent( Event event ) {
component.setInputText( text );
}
else if ( event.id == Event.KEY_RELEASE && event.target == Width )
{
int old_width, width;
old_width = component.getWidth();
19
try {
try {
component.setWidth( width );
} catch( PropertyVetoException e ) {
} catch( NumberFormatException e ) {
changeAgent.addPropertyChangeListener( l ); }
{ changeAgent.removePropertyChangeListener( l ); }
PropertyChangeSupport( this ); }
The handleEvent() method is called by AWT when the user enters data
20
Providing Alternative Behavior in Non-GUI Environments
• A GUI interface is not always available to a Bean especially when the
• A Bean will return true for this method if it is actively avoiding the GUI
environment
• The method is used by the container to tell the Bean that even though
21
A container using a Bean on a server can call this method to tell the
GUI environment
• It is safe to return true and then never use the GUI environment rather