What is JavaBeans?

0
24
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

What are JavaBeans?

JavaBeans is a software component model for java. The JavaBeans API specifications define beans as -A Java Bean is a reusable component that can be manipulated visually in any bean builder tool.

To work with java bean we need to import java.beans package. Any bean builder tool will be able to manipulate beans. They will provide the ability to introspect them.

JavaBean API defines a set of naming conventions for the methods that a bean defines. If a bean follows these conventions, a builder tool can use the new reflection APIs to determine what properties, methods, and events that bean supports.

The introspector class uses reflection to obtain information about a bean and presents it to the builder tool in the form of a BeanInfo object which contains various FeatureDescriptor objects, describing the properties, methods, and events of the bean.

The JavaBean API contains classes and interfaces intended for the use of the codes who are designing the beans for others.

One interesting feature of JavaBeans API is that there is no Bean class that all beans must extend. The Java beans classes that a bean designer uses are generally auxiliary classes, used not by the bean but by the builder tool that manipulates the bean.

JavaBeans API can be used by coders who are assembling an application using beans. Coders may update it manually or use a builder tool. Coders using bean do not typically have to use the java.beans package. Coders need to know the event model used by the beans. Also, they need to be familiar with the naming convention.

When we ship the bean, we need to bundle it together with those auxiliary classes with all required but additional details. Besides a BuildInfo class, complex beans may also provide a customized class and property descriptor for classes. A customized class is a kind of configuration tool or wizard for beans. It just guides the user through the bean customization.

PropertyEditor class is used to allow users to edit the values of the bean properties of a particular class. Builder tools have built-in property editors for common types Like Strings, Colors, and font. But a bean that has properties of some unusual or custom type may want to provide a PropertyEditor subclass to allow the user to easily specify values for the properties.

 Packaging a bean

To create or prepare a bean, we can use a bean box tool. We need to create a package in a jar file along with the other classes or resources it needs. A single bean can contain many auxiliary files and a single jar file may contain multiple beans.

For this, we need to use the manifest file properly for a jar. The manifest file must define which of the jar file entries are beans. Use partial manifest file option to jar it. To identify a class file as bean, we need to add the following line in the manifest file

Java-Bean:True

To package multilevel class in jar file provide proper structure:

MyTest/beans/Myclass.class
Java-Bean:True

Installing a Bean

A bean can be installed using a beanbox. The Beanbox tool is available with BDK(Bean Development Kit). We need to copy the bean’s jar files into jars/directory within the BDK directory. Alternatively, we can use the Load JAR option from the File menu of beanbox.

Beans class in Java

The Beans class is never meant to be instantiated; its static methods provide miscellaneous JavaBeans features. The instantiate() method creates an instance of a bean. The specified bean name represents either a serialized bean file or a bean class file; it is interpreted relative to the specified ClassLoader object.

The setDesignTime() and isDesignTime() methods are used to set and query a flag that indicates whether beans are being used in an application builder environment. Similarly, setGuiAvailable() and isGuiAvailable() set and query a flag that indicates whether the Java Virtual Machine is running in an environment in which a GUI is available. (untrusted applet code cannot call setDesignTime() or setGuiAvailable().)

The isInstanceOf() method is a replacement for the Java instanceof operator for use with beans. Currently, it behaves just like instanceof, but in the future, it may work with beans that consist of a set of Java objects, each of which provides a different ” view ” of a bean.

Similarly, the getInstanceof() method is a replacement for the Java cast operator. It converts a bean to a superclass or interface type. Currently, it behaves just like a cast, but we should use it for future compatibility with multiclass beans.

The structure of Bean class is given as

public class java.beans.Beans extends java.lang.Object {
// Default Constructor: 
public Beans ()
// Class Methods
public static Object getInstanceOf (Object bean, class targetType);
public static Object instantiate(ClassLoader cls, String beanName)
public static Object instantiate 'u' throws IOException,ClassNotFoundException;
public static boolean isDesignTime();
public static boolean isGuiAvailable();
public static boolean isInstanceOf(Object bean, class targetType);
public static void set DesignTime(boolean isDesign Time) throws SecurityException;
public static void setGuiAvailable (boolean isGuiAvailable) throws SecurityException;
}

VetoableChangeListener in java bean

VetoableChangeListener interface is an extension of java.util.EventListener and defines the method that a class must implement in order to be notified when a Java bean makes a change to a “constrained” property.

A PropertyChangeEvent is passed to the vetoableChange() method when such a change occurs, and if the VetoableChangeListener wants to prevent the change from occurring, this method should throw a PropertyVetoException.

The structure of the VetoableChangeListener interface is given as

public abstract interface java.beans.VetoableChangeListener extends java.util.EventListener{
// Public Instance Methods
public abstract void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException;
}

PropertyVetoException in java bean

The PropertyVetoException signals that a VetoableChangeListener that received a PropertyChangeEvent for a ” constrained ” property of a bean has vetoed that proposed change.

When this exception is received, the property in question should revert back to its original value, and any VetoableChangeListener objects that have already been notified of the property change must be re – notified to indicate that the property has reverted to its old value.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here