Sie sind auf Seite 1von 5

JAVA BEANS:

The Java reusable objects whose methods and properties can be


determined by an application at run time and customize its properties
using the methods available for that object.
The Bean-Writing Process:
• To use a bean object in variety of applications, the bean object should
have the properties that can be change by the application programmer.
• The class definition must have the following minimum characteristics to
qualify as a bean object:
1. They should have a public default constructor
2. They should implement the Serializable interface if the bean state
has to be stored
3. They should have methods that allow an application builder to
customize their properties.
4. To set or retrieve the values of fields of the class, the definition
should contain set and get methods, commonly known as getters
and setters. This will confirm the design pattern of a bean.
5. Now, if a bean class has an identifier Id and wants to alter the
values by application developer, the class must have the methods:
<Id type> getId() { return Id;}
void setId(<Id type> i) { Id = i; }

Using Beans to Build an Application:


• First, we have to pack our bean object so that it can be easily import to
the application builder tools.
o jar tool is used to pack the bean class files. A jar file needs a
manifest file which will define the class files which should be
included in toolbox.

Manifest-Version: 1.0

Name: com/class_dir/first.class
Java-Bean: true

Name: com/class_dir/second.class
Java-Bean: true

o Run the jar utility as follows:


 jar cvfm jar_file manifest_file class_files

o cvfm

1
 Create a new archive(c),
 Produce verbose output when creating the jar(v)
 Name of the jar file should be first file name that follows
the class files(f).
 Given Manifest file for creating the jar file(m).
o Now a builder tool can add bean in the application.
o Builder tools also contain a property editor tool and event
descriptor, by which we can specify our values and events for a
beans to customize it.

Naming Patters for Bean Properties and Events:


• To learn the properties and events of a bean, the class must follow
some naming conventions to specify properties and events.
• Alternatively, the bean write can apply a bean information class that
tells the builder tool about the properties and events of the bean.
• The naming patter for properties is simple: Any pair of methods
public type getProperty Name()
public void setPropertyName(type newValue)
corresponds to a read/ write property.
• If a get method is not associated with set method then the property
will be treated as read-only property.
• Likewise, if a set method is not associated with get method then the
property will be write-only property.
• Boolean property type should have is/set naming pattern:

public boolean isProperty Name()


public void setPropertyName(boolean newVal)

• All event class must end in Event, and classes must extend the
EventObject class.

public void addEventNameListener


(EventNameListener e)
public void removeEventNameListener
(EventNameListener e)

Bean Property Type:


• Depending upon the complexity of the properties of a bean, the bean
specification allows four types of properties:

 Simple Properties: In which a value is updated or retrieve


by set/ get property method.
 Indexed Properties:In which any array is updated or array

2
value is retrieved.
 Bound Properties:Any change in bound property involves
some kind of action. Like a text field is bound
to a resultset value and any change in resultset
will lead to a change in text field value.
 To implement this type of properties, the bean must send a
PropertyChange event to all register listeners.
 The class PropertyChangeSupport manages to add or remove
any interested listener of the bean.
private PropertyChangeSupport changeSupport
= new PropertyChangeSupport(this);
 To register the interested listener, bean has to implement
following two methods:

public void addPropertyChangeListener


(PropertyChangeListener listener)
{
changesupport.addPropertyChangeListener
(listener);
}

public void removePropertyChangeListener


(PropertyChangeListener listener)
{
changesupport.addPropertyChangeListener
(listener);
}

 Now to notify all the register listener, bean has to call


‘firePropertyChange()’ method of PropertyChnageSupport class
within set property method.

 Constrained Properties: It might happed that any listener


can deny the changes made by set method
and forcing it to retain the old value. For
example a frame close event can deny it’s
closing without saving it’s contents.
 To build a constrained property your bean must implement the
following two methods to manage vetobleChangeListener
object:

public void addVetoableChangeListener


(VetoableChangeListener listener);

3
public void removeVetoableChangeListener
(VetoableChangeListener listener);

 Like PropertyChaneSupport class there is another class in


java.beans package VetoableChangeSupport class, that manages
the VetoableChangeListeners.

BeanInfo Classes:
• In a complex bean description, it may include set/ get method for other
purposes or bean couldn’t follow the standard naming convention.
• An object which implements BeanInfo interface can be used to
describe the naming pattern for a particular bean for use by a builder.
• The class name must suffix with ‘BeanInfo’.
• You can also extend SimpleBeanInfo convenience class to avoid
writing all the methods of BeanInfo interface.

Property Edi tor:


• Some properties can not be changed directly from property pane, it
requires more specific value. For example a color property, this
requires a color palate to choose a color. These separate components
are called property editors.
• The s teps to create a new property editor:
o Create a BeanInfo class.
o Override the getPropertyDescriptors method. This will return an
array of PropertyDescriptor objects.
o Create one object for each property that should be displayed on
a property editor.
• The registerEditor method of the PropertyEditorManager class sets a
property editor for all properties of a given type.

4
Writing a Property Editor:
• One property editor is required for each property
• Property editor must implement the PropertyEditor interface
containing 12 methods.
• We can use PropertyEditorSupport class that is supplied with the
standard library.
• Builder follow this procedure to display the properties:
o It instantiate property editor for each property of the bean.
o It asks the bean to tell it the current value of the property.
o It then asks the property editor to display the value.
• The values can be displayed in either GUI based or text based.
 Simple Property Editor:
• We use some mnemonics to define the property value, for eg. Text
size can be defined in one of the five choices as 1 to 5 digits.
• Now your code will return a wrapper class to return the selected value.
 GUI based Property Editor:
• When the user clicks on the property editor, a pop up box comes, this
contains the sample text/ images for the selected values.
• The dialog box contains a component to edit the property value.
• To build a GUI based editor:
o Tell the builder tool that you will paint the value and not use a
string.
o “Paint” the value the user enters onto the GUI.
o Tell the builder tool that you will be using GUI-based property
editor.
o Build the GUI.
o Write the code to validate when the user tries to enter as the
value.
Customizers:
• It is better to supply one property editor for multiple property instead
of supplying multiple GUI based property editor for each property.
• To write customizer class , you must implement a Customizer
interface which has three methods:
o setObject method, which takes a parameter that specifies the
bean being customized.
o addPropertyChangeListener and
removePropertyChangeListener methods to notify the changes.

Das könnte Ihnen auch gefallen