User:Rakeshmku

JavaBeans Concepts The JavaBeans™ architecture is based on a component model which enables developers to create software units called components. Components are self-contained, reusable software units that can be visually assembled into composite components, applets, applications, and servlets using visual application builder tools. JavaBean components are known as beans. A set of APIs describes a component model for a particular language. The JavaBeans API specificationdescribes the core detailed elaboration for the JavaBeans component architecture. Beans are dynamic in that they can be changed or customized. Through the design mode of a builder tool you can use the Properties window of the bean to customize the bean and then save (persist) your beans using visual manupulation. You can select a bean from the toolbox, drop it into a form, modify its appearance and behavior, define its interaction with other beans, and combine it and other beans into an applet, application, or a new bean. The following list briefly describes key bean concepts. •	Builder tools discover a bean's features (that is, its properties, methods, and events) by a process known as introspection. Beans support introspection in two ways: o	By adhering to specific rules, known as design patterns, when naming bean features. The Introspector class examines beans for these design patterns to discover bean features. The Introspector class relies on the core reflection API. The trail The Reflection API is an excellent place to learn about reflection. o	By explicitly providing property, method, and event information with a related bean information class. A bean information class implements the BeanInfo interface. A BeanInfo class explicitly lists those bean features that are to be exposed to application builder tools. •	Properties are the appearance and behavior characteristics of a bean that can be changed at design time. Builder tools introspect on a bean to discover its properties and expose those properties for manipulation. •	Beans expose properties so they can be customized at design time. Customization is supported in two ways: by using property editors, or by using more sophisticated bean customizers. •	Beans use events to communicate with other beans. A bean that is to receive events (a listener bean) registers with the bean that fires the event (a source bean). Builder tools can examine a bean and determine which events that bean can fire (send) and which it can handle (receive). •	Persistence enables beans to save and restore their state. After changing a bean's properties, you can save the state of the bean and restore that bean at a later time with the property changes intact. The JavaBeans architecture uses Java Object Serialization to support persistence. •	A bean's methods are no different from Java methods, and can be called from other beans or a scripting environment. By default all public methods are exported. Beans vary in functionality and purpose. You have probably met some of the following beans in your programming practice: •	GUI (graphical user interface) •	Non-visual beans, such as a spelling checker •	Animation applet •	Spreadsheet application Using the NetBeans GUI Builder This lesson explains how to use the NetBeans IDE GUI Builder to work with beans. In preparation for working with the GUI Builder, you should be first familiar with the key NetBeans concepts which are explained in the Using NetBeans IDE Guide. This lesson guides you through the process of creating a bean pattern in the NetBeans projects, introduces the user interface of the GUI Builder, and explains how to add your bean object to the palette. Creating a New Project In the NetBeans IDE, you always work in a project where you store sources and files. To create a new project, perform the following steps: 1.	Select New Project from the File menu. You can also click the New Project button in the IDE toolbar. 2.	In the Categories pane, select the General node. In the Projects pane, choose the Java Application type. Click the Next button. 3.	Enter MyBean in the Project Name field and specify the project location. Do not create a Main class here, because later you will create a new Java class in this project. 4.	Click the Finish button. This figure represents the expanded MyBean node in the Projects list. Creating a New Form After creating a new project, the next step is to create a form within which the JavaBeans components and other required GUI components, will be placed. To create a new form, perform the following sequence of actions: 1.	In the Projects list, expand the MyBean node, right-click on the node and choose New|JFrame Form from the pop-up menu. 2.	Enter MyForm as the Class Name. 3.	Click the Finish button. The IDE creates the MyForm form and the MyForm class within the MyBean application and opens the MyForm form in the GUI Builder. The GUI Builder Interface When the JFrame form is added to your application, the IDE opens the newly-created form in an Editor tab with a toolbar containing the following buttons: •	Selection Mode enables you to select one or more objects in the Design Area. •	 Connection Mode enables you to set a connection between objects by specifying an event. •	 Preview Design enables you to preview the form layout. •	 Align commands enable you to align selected objects. •	 Change Resizability enables you to set up vertical and horizontal resizing. When the MyForm form opens in the GUI Builder's Design view, three additional windows appear, enabling you to navigate, organize, and edit GUI forms. These windows include the following: •	Design Area. The primary window for creating and editing Java GUI forms. Source and Design toggle buttons enable you to switch between view a class's source code and a graphical view of the GUI components. Click on an object to select it in the Design Area. For a multiple selection, hold down the Ctrl key while clicking on objects. •	Inspector. Representation of a tree hierarchy of all the components in your application. The Inspector highlights the component in the tree that is currently being edited. •	Palette. A customizable list of available components containing groups for Swing, AWT, Borders, and Beans components. This window enables you to create, remove, and rearrange the categories displayed in the palette using the customizer. •	Properties Window. A display of the properties of the component currently selected in the GUI Builder, Inspector window, Projects window, or Files window. Creating a Bean To create your own bean object and add it to the palette for the bean group, execute the following procedure: 1.	Select the node in the MyBean project. 2.	Choose New|Java Class from the pop-up menu. 3.	Specify the name for the new class, for example, MyBean, then press the Finish button. 4.	Expand the MyBean.java and MyBean node and select the Bean Patterns node. 5.	Right-click on the Bean Patterns node and choose Add|Property from the pop-up menu. 6.	Enter YourName in the PropertyName field as shown in the following figure, then press OK: 7.	Right-click the MyBean node in the MyBean project tree and choose Tools |Add to Palette from the pop-up menu. 8.	Select the Beans group in the Palette tree to add your bean. Adding Components to the Form Now you can use the Free Design of the GUI Builder and add the MyBean component and other standard Swing components to MyForm. 1.	Select the MyForm node in the project tree. 2.	Drag the JLabel Swing component from the Palette window to the Design Area. Double-click the component and change the text property to "Enter your name:". 3.	Drag the JTextField component from the Palette window to the Design Area. Double-click the component and empty the text field. 4.	Drag the JButton component from the Palette window to the Design Area. Double-click the component and enter "OK" as the text property. 5.	Add another button and enter "Cancel" as its text property. 6.	Align components by using the appropriate align commands. 7.	Before you drag the MyBean component from the Pallete you must compile your project because the MyBean component is non-visual and cannot be operated as a visual component. When you Drag and Drop the MyBean component it will not appear in the Design Area. However, you can view it in the Inspector window by expanding the Other Components node Properties In the following sections you will learn how to implement bean properties. A bean property is a named attribute of a bean that can affect its behavior or appearance. Examples of bean properties include color, label, font, font size, and display size. The JavaBeans™ specification defines the following types of bean properties: •	Simple – A bean property with a single value whose changes are independent of changes in any other property. •	Indexed – A bean property that supports a range of values instead of a single value. •	Bound – A bean property for which a change to the property results in a notification being sent to some other bean. •	Constrained – A bean property for which a change to the property results in validation by another bean. The other bean may reject the change if it is not appropriate. Bean properties can also be classified as follows: •	Writable – A bean property that can be changed o	Standard o	Expert o	Preferred •	Read Only – A bean property that cannot be changed. •	Hidden – A bean property that can be changed. However, these properties are not disclosed with the BeanInfo class BeanBuilder uses this schema to group and represent properties in the Properties window. Manipulating Events Event passing is the means by which components communicate with each other. Components broadcast events, and the underlying framework delivers the events to the components that are to be notified. The notified components usually perform some action based on the event that took place. The event model was designed to accommodate the JavaBeans™ architecture. To understand how events and event handling work in the JavaBeans component model, you must understand the concepts of events, listeners, and sources. To refresh your knowledge in these areas, read the Writing Event Listeners lesson of the Swing tutorial. The event model that is used by the JavaBeans architecture is a delegation model. This model is composed of three main parts: sources, events, and listeners. The source of an event is the object that originates or fires the event. The source must define the events it will fire, as well as the methods for registering listeners of those events. A listener is an object that indicates that it is to be notified of events of a particular type. Listeners register for events using the methods defined by the sources of those events. Classes That Are Serializable Any class is serializable as long as that class or a parent class implements the java.io.Serializable interface. Examples of serializable classes include Component, String, Date, Vector, and Hashtable. Thus, any subclass of the Component class, including Applet, can be serialized. Notable classes not supporting serialization include Image, Thread, Socket, and InputStream. Attempting to serialize objects of these types will result in an NotSerializableException. The Java Object Serialization API automatically serializes most fields of a Serializable object to the storage stream. This includes primitive types, arrays,and strings. The API does not serialize or deserialize fields that are marked transient or static. Controlling Serialization You can control the level of serialization that your beans undergo. Three ways to control serilization are: •	Automatic serialization, implemented by the Serializable interface. The Java serialization software serializes the entire object, except transient and static fields. •	Customized serialization. Selectively exclude fields you do not want serialized by marking with the transient (or static) modifier. •	Customized file format, implemented by the Externalizable interface and its two methods. Beans are written in a specific file format. Default Serialization: The Serializable Interface The Serializable interface provides automatic serialization by using the Java Object Serialization tools. Serializable declares no methods; it acts as a marker, telling the Object Serialization tools that your bean class is serializable. Marking your class Serializable means you are telling the Java Virtual Machine (JVM) that you have made sure your class will work with default serialization. Here are some important points about working with the Serializable interface: •	Classes that implement Serializable must have an access to a no-argument constructor of supertype. This constructor will be called when an object is "reconstituted" from a .ser file. •	You don't need to implement Serializable in your class if it is already implemented in a superclass. •	All fields except static and transient fields are serialized. Use the transient modifier to specify fields you do not want serialized, and to specify classes that are not serializable. Selective Serialization Using the transient Keyword To exclude fields from serialization in a Serializable object from serialization, mark the fields with the transient modifier. transient int status; Default serialization will not serialize transient and static fields. Selective Serialization: writeObject and readObject If your serializable class contains either of the following two methods (the signatures must be exact), then the default serialization will not take place. You can control how more complex objects are serialized, by writing your own implementations of the writeObject and readObject methods. Implement writeObject when you need to exercise greater control over what gets serialized when you need to serialize objects that default serialization cannot handle, or when you need to add data to the serialization stream that is not an object data member. Implement readObject to reconstruct the data stream you wrote with writeObject. Purpose of Introspection A growing number of Java object repository sites exist on the Internet in answer to the demand for centralized deployment of applets, classes, and source code in general. Any developer who has spent time hunting through these sites for licensable Java code to incorporate into a program has undoubtedly struggled with issues of how to quickly and cleanly integrate code from one particular source into an application. The way in which introspection is implemented provides great advantages, including: 1.	Portability - Everything is done in the Java platform, so you can write components once, reuse them everywhere. There are no extra specification files that need to be maintained independently from your component code. There are no platform-specific issues to contend with. Your component is not tied to one component model or one proprietary platform. You get all the advantages of the evolving Java APIs, while maintaining the portability of your components. 2.	Reuse - By following the JavaBeans design conventions, implementing the appropriate interfaces, and extending the appropriate classes, you provide your component with reuse potential that possibly exceeds your expectations. Introspection API The JavaBeans API architecture supplies a set of classes and interfaces to provide introspection. The BeanInfo (in the API reference documentation) interface of the java.beans package defines a set of methods that allow bean implementors to provide explicit information about their beans. By specifying BeanInfo for a bean component, a developer can hide methods, specify an icon for the toolbox, provide descriptive names for properties, define which properties are bound properties, and much more. The getBeanInfo(beanName) (in the API reference documentation) of the Introspector (in the API reference documentation) class can be used by builder tools and other automated environments to provide detailed information about a bean. The getBeanInfo method relies on the naming conventions for the bean's properties, events, and methods. A call to getBeanInfo results in the introspection process analyzing the bean’s classes and superclasses. The Introspector class provides descriptor classes with information about properties, events, and methods of a bean. Methods of this class locate any descriptor information that has been explicitly supplied by the developer through BeanInfo classes. Then the Introspector class applies the naming conventions to determine what properties the bean has, the events to which it can listen, and those which it can send. Each class represented in this group describes a particular attribute of the bean. For example, the isBound method of the PropertyDescriptor class indicates whether a PropertyChangeEvent event is fired when the value of this property changes. Editing Bean Info with the NetBeans BeanInfo Editor To open the BeanInfo dialog box, expand the appropriate class hierarchy to the bean Patterns node. Right-click the bean Patterns node and choose BeanInfo Editor from the pop-up menu. All elements of the selected class that match bean-naming conventions will be displayed at the left in the BeanInfo The following attributes are available for the nodes for each bean, property, event sources, and method: •	Name - A name of the selected element as it appears in code. •	Preferred - An attribute to specify where this property appears in the Inspector window under the Properties node. •	Expert - An attribute to specify where this property appears in the Inspector window under the Other Properties node. •	Hidden - An attribute to mark an element for tool use only. •	Display Name Code - A display name of the property. •	Short Description Code - A short description of the property. •	Include in BeanInfo - An attribute to include the selected element in the BeanInfo class. •	Bound - An attribute to make the bean property bound. •	Constrained - An attribute to make the bean property constrained. •	Mode - An attribute to set the property's mode and generate getter and setter methods. •	Property Editor Class - An attribute to specify a custom class to act as a property editor for the property. Customization provides a means for modifying the appearance and behavior of a bean within an application builder so it meets your specific needs. There are several levels of customization available for a bean developer to allow other developers to get maximum benefit from a bean’s potential functionality. A bean's appearance and behavior can be customized at design time within beans-compliant builder tools. There are two ways to customize a bean: •	By using a property editor. Each bean property has its own property editor. The NetBeans GUI Builder usually displays a bean's property editors in the Properties window. The property editor that is associated with a particular property type edits that property type. •	By using customizers. Customizers give you complete GUI control over bean customization. Customizers are used where property editors are not practical or applicable. Unlike a property editor, which is associated with a property, a customizer is associated with a bean. Customizers You have learned that builder tools provide support for you to create your own property editors. What other needs should visual builders meet for complex, industrial-strength beans? Often it is undesirable to have all the properties of a bean revealed on a single (sometimes huge) property sheet. What if one single root choice about the type of the bean rendered half the properties irrelevant? The JavaBeans specification provides for user-defined customizers, through which you can define a higher level of customization for bean properties than is available with property editors. When you use a bean Customizer, you have complete control over how to configure or edit a bean. A Customizer is an application that specifically targets a bean's customization. Sometimes properties are insufficient for representing a bean's configurable attributes. Customizers are used where sophisticated instructions would be needed to change a bean, and where property editors are too primitive to achieve bean customization. All customizers must: •	Extend java.awt.Component or one of its subclasses. •	Implement the java.beans.Customizer interface This means implementing methods to register PropertyChangeListener objects, and firing property change events at those listeners when a change to the target bean has occurred. •	Implement a default constructor. •	Associate the customizer with its target class via BeanInfo.getBeanDescriptor.