You are on page 1of 11

Java Beans

• jar cf jar-file input-file(s)


• The options and arguments used in this command are:
• The c option indicates that you want to create a JAR file.
• The f option indicates that you want the output to go to a file rather than to
stdout.
• jar-file is the name that you want the resulting JAR file to have. You can use
any filename for a JAR file. By convention, JAR filenames are given a .jar
extension, though this is not required.
• The input-file(s) argument is a space-separated list of one or more files that
you want to include in your JAR file. The input-file(s) argument can contain
the wildcard * symbol. If any of the "input-files" are directories, the contents
of those directories are added to the JAR archive recursively.
• The c and f options can appear in either order, but there must not be any
space between them.
• This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest file for
the JAR archive.
Viewing the Contents of a JAR File

• The basic format of the command for viewing the contents of a JAR
file is: jar tf jar-file Let's look at the options and argument used in
this command:
• The t option indicates that you want to view the table of contents of
the JAR file.
• The f option indicates that the JAR file whose contents are to be
viewed is specified on the command line.
• The jar-file argument is the path and name of the JAR file whose
contents you want to view.
• The t and f options can appear in either order, but there must not be
any space between them.
• This command will display the JAR file's table of contents to stdout.
• You can optionally add the verbose option, v, to produce additional
information about file sizes and last-modified dates in the output.
• Creating a simple bean
• Compiling the bean
• Generating a Java Archive (JAR) file
• Loading the bean into the GUI Builder of
the NetBeans IDE
• Inspecting the bean's properties and
events
Advantages of Java Beans
• A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm. 

• The properties, events, and methods of a Bean that are exposed to an application
builder tool can be controlled.

• A Bean may be designed to operate correctly in different locales, which makes it


useful in global markets.

• Auxiliary software can be provided to help a person configure a Bean. This software is only
needed when the design-time parameters for that component are being set. It
does not need to be included in the run-time environment.

• 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 events that are
sent to other objects.
Introspection
• This is the process of analyzing a bean to
determine its capabilities.
• Simple properties
• Indexed properties
Using the Bean Info Interface
• Property Descriptor[ ] getPropertyDescriptors()
• EventSetDescriptor[ ] getEventSetDescriptors()
• MethodDescriptor[ ] getMethodDescriptors()

• These are defined with java.beans package and they describe the indicated
elements.By implementing these methods a developer can designate
exactly what is presented to a user bypassing introspection based on design
patterns.
• When creating a class that implements BeanInfo class name should
ClassNameBeanInfo.
• It provide default implementation of all three methods.
• import java.awt.*;
• import java.beans.*;

• public class IntrospectorDemo {

• public static void main(String[] args) {


• try{
• Class c=Class.forName("Colors");
• BeanInfo beanInfo=Introspector.getBeanInfo(c);
• System.out.println("Properties");
• PropertyDescriptor propertyDescriptor[]=beanInfo.getPropertyDescriptors();
• for(int i=0;i<propertyDescriptor.length;i++){
• System.out.println("\t"+propertyDescriptor[i].getName());
• }
• System.out.println("Events");
• EventSetDescriptor eventSetDescriptor[]=beanInfo.getEventSetDescriptors();
• for(int i=0;i<eventSetDescriptor.length;i++){
• System.out.println("\t"+eventSetDescriptor[i].getName());

• // System.out.println("Methods");
• // MethodDescriptor
methodDescriptor[]=beanInfo.getMethodDescriptors();
• // for(int j=0;i<methodDescriptor.length;j++){
• // System.out.println("\t"+methodDescriptor[j].getName());
• //
• //}
• }
• }
• catch(Exception e)
• {
• System.out.println("Exception caught"+e);
• }
• }
• }
• import java.beans.*;
• public class ColorsBeanInfo extends SimpleBeanInfo{
• @Override public PropertyDescriptor[] getPropertyDescriptors()
• {
• try
• {
• PropertyDescriptor rectangular=new
• PropertyDescriptor("Circle",Colors.class);
• PropertyDescriptor pd[]={rectangular};
• return pd;
• }
• catch(Exception e)
• {
• System.out.println("Exception caught"+e);
• }
• return null;
• }

• }
• Bound and Constrained properties:Must Implement a
PropertyChangeListner interface.

• Persistance:must implement serializable


Restriction:Must supply parameterless constructor
Avoid serialization by using transient(non-access modifier) keyword.

• Customizers

• JavaBeans API

You might also like