javabeans by agni

Upload: agnihotra

Post on 30-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 JavaBeans by Agni

    1/4

    Java Beans

    Introduction to JavaBeans:

    The specificat ion by Sun Microsystems defines JavaBeans as reusable softwarecomponentsthat can be manipulated visually in a builder tool.

    The JavaBeans archit ecture is based on a component model which enablesdevelopersto create software unit scalled components. Components are self-contained, reusable softwareunits that can be visually assembled into composite components, applets, applications andservlets using visual application builder tool. JavaBeans components are known as beans.Every Java bean component should follow JavaBeans specificat ion, which define the way bywhich different components can communicate with each other, interact with a builder tool,save/restore their configuration etc. In addition to this it also defines the architecture of a singleclass in the software component.

    A JavaBean is a simple java class, adhering to certain conventions about methodnaming constructorsand behaviors. The required conventions for JavaBeans are:

    I t should have a no-argument constructor so that its object can be created withoutknowing any thing about bean.

    Its properties must be accessible using getter/setter and other methods (collectivelyknown as accessor/methods) following a standard naming convention. As the nameimplies, getter method is used t o obtain the value of a p roperty, while setter method isused to set the value of property.

    Advantages of Java Beans:

    A software component architecture provides standards mechanism to deal withsoftware building blocks. The following list enumerated some of the specific benefits that javatechnology provides for a component developer.

    Instead of reusing Java Classes (Write-Once-Run-Anywhere WORA) a bigger size of

    code (component) is reused (Write-Once-Deploy-Anywhere WODA). Thereby takingcode reuse to a while new level. The properties, events, and methods of a Bean that are exposed to an application

    builder tool can be controlled. The configuration settings of a bean can be saved in persistent storage and restored at

    a later time. A Bean may register to receive events from other Objects and can generate eventsthat

    are sent to other objects, thereby making it useful for Interactive applications. Bean can be configured by using auxiliary software of users, choice.

  • 8/14/2019 JavaBeans by Agni

    2/4

    Create and Configure an Instance of the OurButton Bean:

    Follow these steps to create and configure an instance of the OurButton Bean andconnect it to the Molecule Bean:

    Position the cursor on the ToolBox entry labeled OurButton and click the left mousebutton. You should see the cursor change to a cross. Move the cursor to the BeanBox display area and click the left mouse button in

    approximately the area where you wish the Bean to be displayed. You should see arectangular region appear that contains a button. Thisarea issurrounded

    by a hatched border indicating that it iscurrently selected. You may reposition the OurButton Bean by positioning the cursor over one of the

    hatched bordersand dragging the Bean. Go to the Properties window and change the label of the Bean to Rotate X. The

    button appearance changesimmediately when this property is changed. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed.

    You should now see a line extending from the button to the cursor. Notice that one endof the line moves as the cursor moves. However, the other end of the line remains fixedat the butt on.

    Move the cursor so that it is inside the Molecule Bean display area, and click the leftmouse button. You should see the Event Target Dialog dialog box.

    The dialog box allows you to choose a method that should be invoked when this buttonis clicked. Select the entry labeled rotateOnX and click the OK button. You should seea message box appear very briefly, stating that the tool is Generating and compilingadaptor class.

    Test the application. Each time you press the button, the molecule should move a fewdegrees around one of its axes.

    Now create another instance of the OurButton Bean. Label it Rotate Y and map its

    action event to the rotateY method of the Molecule Bean. The steps to do this are verysimilar to those just described for the button labeled Rotate X.Test the application by clickingthese but tonsand observing how the molecule moves.

    Introspection: Introspection is the automatic process of analyzing a beans design patterns to reveal

    the beans propert ies, events and methods. Introspection asks JavaBeans components what arethe propert ies and events it supports. The JavaBeans API providesa great deal of Support forintrospection so as to enable the application builder tool to find out information about thatstructures and functionality of a bean , we cannot think of JavaBeans technology withoutconsidering introspection mechanism. There are two ways of support introspection. In the first

    method of introspection is supported by reflection where you name methods with certainconventions, like set/getProperty() and add/Listener(). In the second method you explicitlyexpose the beans behavior through a class which extends SimpleBeanInfo which in turnprovidesdefault implementation of BeanInfo interface.

  • 8/14/2019 JavaBeans by Agni

    3/4

    Developing a Simple Bean using the BDK:

    Here are the stepsthat you must follow to create a new Bean:

    o Create a directory for the new Bean.c:\bdk\demo\sunw\demo\colors.

    o Create the Java source file(s).o Compile the source file(s).

    javac Colors.java.o Create a manifest file.

    Name: sunw/demo/colors/Colors.classJava-Bean: True In the location :c:\bdk\demo\colors.mft.

    o Generate a JAR file. jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class

    o Start the BDK.o Test.

    Create and Configure an Instance of the OurButton BeanCreate an instance of the OurButton Bean in the BeanBox window. Then followthese steps:

    1. Go to the Properties window and change the label of the Bean to Change. Youshould see that the button appearance changesimmediately when this property ischanged.

    2. Go to the menu bar of the BeanBox and select Edit | Events| action | actionPerformed.3. Move the cursor so that it is inside the Colors Bean display area, and click the left mouse

    but ton. You should see the Event Target Dialog dialog box.4. The dialog box allows you to choose a method that should be invoked when thisbutton

    is clicked. Select the entry labeled change and click the OK button. You should see amessage box appear very briefly, stating that the tool is Generating and compilingadaptor class.

    5. Click on the button. You should see the color change.

    Bound Propert ies:

    Sometimes when a Bean property changes, another object might need to be notified ofthe change and react to the change, we can do this by making a property bound property .Therefore when a bound property changes, notification of the change is sent to the interestedlistenersthrough java.beans.PropertyChangeEvent class.

    Bound properties support java.beans.PropertyChangeListener so as to receivePropertyChangeEvent notifications. The accessor methods for a bound property are defined inthe same way as those for simple properties. However you also provide the event listenerregistration methods for PropertyChangeListener classes and fire a PropertyChangeEvent event to the PropertyChangeListener object by calling their PropertyChange methods. Theconvenience java.beans.PropertyChangeSupport class enables your bean to implement thesemethods. In order to listen the property change, an object must be able to add and removeitself from listener list in the bean containing the bound property. It must also be able torespond to the event notificat ion method that signalsa property change.

  • 8/14/2019 JavaBeans by Agni

    4/4

    Create an instance of the TickTock Bean. The Properties window should show oneproperty for this component. It is Interval and its initial value is 5. This representsthe numberof seconds that elapse between property change eventsgenerated by the TickTock Bean.Change the value to 1.

    Now you need to map events generated by the TickTock Bean into method callsonthe Colors Bean. Follow these steps:

    Go to the menu bar of the BeanBox and select Edit | Events| propertyChange |propertyChange. You should now see a line extending from t he button to the cursor.

    Move the cursor so that it is inside the Colors Bean display area, and click the left mousebut ton. You should see the Event Target Dialog dialog box.

    The dialog box allows you to choose a method that should be invoked when thiseventoccurs. Select the entry labeled change and click the OK button. You should see amessage box appear very briefly, stating that the tool is Generating and compilingadaptor class.

    You should now see the color of your component change every second.

    Constrained Properties:

    Constrained propertiesare more complicated that bound properties because they also

    support property change listener which happen to be vetoers. i.e., the listener may accept theproperty change or forbid it.A bean property is constrained it the bean supports the

    java.beans.VetoableChangeListener and java.bean.PropertyChangeEvent classes and if the setmethod for this property throws a java.beans.PropertyVetoException .

    The accessor methods for a constrained property are defined in the same way as thosefor simple properties, with the addition that the setXXX method throws aPropertyVetoException.

    public void setPropertyName(PropertyType value)throws PropertyVetoException{. . .}

    Persistence:

    Persistence means storing the data in a durable space so that is can be accessed at alater time by the same application or another. In persistence data outlives the process thatcreated it. Remember if you want a class to be persisted then it should implement

    java.io.Serializable .In the context of JavaBeans we configure Beans with all its necessary configuration

    setting, so that it can be later restored with same configuration setting.

    Customizes:

    The Propertieswindow of the BDK allows a developer to modify the properties of aBean. However, this may not be the best user interface for a complex component with manyinterrelated properties. Therefore, a Bean developer can provide a customizer that helpsanother developer configure this software. A customizer can provide a step-by-step guidethrough t he process that must be followed to use the component in a specific context. Onlinedocumentation can also be provided. A Bean developer has great flexibility to develop acustomizer that can differentiate hisor her product in the marketplace.