beans slides
TRANSCRIPT
-
8/6/2019 Beans Slides
1/5
JAVA BEANS: The Java reusable objects whose methods and properties can bedetermined by an application at run time and customize its propertiesusing 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 stored3. 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 gettersand setters. This will confirm the design pattern of a bean.
5. Now, if a bean class has an identifier Id and wants to alter thevalues by application developer, the class must have the methods: getId() { return Id;}void setId( 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 beincluded in toolbox.
Manifest-Version: 1.0
Name: com/class_dir/first.classJava-Bean: true
Name: com/class_dir/second.classJava-Bean: true
o Run the jar utility as follows: jar cvfm jar_file manifest_file class_files
o cvfm
1
-
8/6/2019 Beans Slides
2/5
-
8/6/2019 Beans Slides
3/5
value is retrieved. Bound Properties: Any change in bound property involves
some kind of action. Like a text field is boundto 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 aPropertyChange event to all register listeners.The class PropertyChangeSupport manages to add or removeany interested listener of the bean.
private PropertyChangeSupport changeSupport= new PropertyChangeSupport(this);
To register the interested listener, bean has to implementfollowing 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 callfirePropertyChange() method of PropertyChnageSupport classwithin set property method.
Constrained Properties: It might happed that any listener can deny the changes made by set methodand forcing it to retain the old value. For example a frame close event can deny itsclosing without saving its contents.
To build a constrained property your bean must implement thefollowing two methods to manage vetobleChangeListener object:
public void addVetoableChangeListener(VetoableChangeListener listener);
3
-
8/6/2019 Beans Slides
4/5
public void removeVetoableChangeListener(VetoableChangeListener listener);
Like PropertyChaneSupport class there is another class in
java.beans package VetoableChangeSupport class, that managesthe VetoableChangeListeners.
BeanInfo Classes: In a complex bean description, it may include set/ get method for other
purposes or bean couldnt 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 avoidwriting 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, thisrequires a color palate to choose a color. These separate componentsare 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 anarray of PropertyDescriptor objects.
o Create one object for each property that should be displayed ona property editor.
The registerEditor method of the PropertyEditorManager class sets a property editor for all properties of a given type.
4
-
8/6/2019 Beans Slides
5/5
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. Textsize 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, thiscontains 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.
5