1 / 31

Java Beans

Java Beans. Java offers software component development through java Beans Java Beans are based on a software component model for java. The model is designed to allow third party vendors to create and sell Java components that can be integrated into other software products.

danae
Download Presentation

Java Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Beans

  2. Java offers software component development through java Beans • Java Beans are based on a software component model for java. • The model is designed to allow third party vendors to create and sell Java components that can be integrated into other software products.

  3. Java beans is an architecture and platform independent set of classes for creating and using Java software components. • Beans support the software component model which focuses on the use of components and containers.

  4. Advantages • It has write once, run anywhere paradigm • A bean may be designed to operate in different locations which makes it useful in global market. • A bean may register to receive events from other objects and can generate events that are sent to other objects.

  5. Application Builder Tools • A utility that enables you to configure a set of Beans, connect them together and produce a working application. • A palette is provided that lists all the available beans. As additional beans are developed / purchased they can be added to the palette. • Worksheet allows the designer to lay out beans in GUI. • Special editors and customizers allow a bean to be configured. • Commands allow a designer to inquire about the state and behavior of a bean. • Capabilities exist to interconnect beans. • When a collection of beans have been configured and connected, it is possible to save all in a persistent storage area. Later on this information can be used to restore the state of the application.

  6. BDK • Bean Developer Kit • JDK must be installed for BDK. • BDK can be downloaded from sun java site. • Using BDK

  7. Sample Beans • Blue Button • A simple button with background, foreground, label and font properties. • Orange Button • Same as Blue button • Our Button • A grey button with additional font properties. • Explicit Button • Same as Blue button • EventMonitor • A text area that is used to view events as they happen.

  8. Jelly Bean • A simple visual component that draws a colored oval “jelly bean”. • Juggler • Represnts a threaded animated component. • Voter • By default the bean will reject all change requests but change requests will be accepted if the the vetoAll propterty is set to false.

  9. ChangeReporter • A text filed • Molecule • Similar to juggler and accept mouse input. • QuoteMonitor • Uses RMI to contact a remote server. • JDBC Select • Uses JDBC API to connect a database. • BridgeTester • Provides set of property types and events that may be used to test other bean components.

  10. JAR files • BDK expect beans to be packaged within JAR files. • A JAR file allows to deploy a set of classes and their associate resources. • The package java.util.zip contains classes that read and write JAR files.

  11. Manifest Files • Indicate which of the components in a JAR file are java Beans. • A manifest file may reference several .class files. • If a .class file is a java Bean, its entry must be immediately followed by the line “java-Bean: True”.

  12. Creating a jar file • Command jar cf XY.jar *.class *.gif creates XY jar file that contains all .class and all gif files of current directory. Syntax: jar options files

  13. Options • c a new archive is to be created • f first element in file list is the name of the archive that is to be created. • u update existing JAR file • v verbose output • x files are to be extracted from the archive. • 0 don’t use compression.

  14. Properties • It is a subset of Beans state. • Value of properties determine behavior and appearance of component. • 3 types of properties • Simple • Boolean • Indexed

  15. Simple Property • Has a single value • N for name of property • T for its type • public T getN(); • public void setN(T arg); • A read / write have both these • Read only has only get method. • A write only has only set method.

  16. The following listing shows a class that has three read/write simple properties: public class Box { private double depth, height, width; public double getDepth( ) { return depth; } public void setDepth(double d) { depth = d; } public double getHeight( ) { return height; } public void setHeight(double h) { height = h; } public double getWidth( ) { return width; } public void setWidth(double w) { width = w; } }

  17. Boolean Properties • Has value true or false • public boolean isN(); • public boolean getN(); • public void setN(boolean value); public class Line { private boolean dotted = false; public boolean isDotted( ) { return dotted; } public void setDotted(boolean dotted) { this.dotted = dotted; } }

  18. Indexed property • Consists of multiple values. • Public T getN(int index); • Public T[ ] getN(); • Public void setN(int index, T value); • Public void setN(T values[ ]);

  19. public class PieChart { private double data[ ]; public double getData(int index) { return data[index]; } public void setData(int index, double value) { data[index] = value; } public double[ ] getData( ) { return data; } public void setData(double[ ] values) { data = new double[values.length]; System.arraycopy(values, 0, data, 0, values.length); } }

  20. Events • public void addTListener(TListener el); • For multiple listeners • public void addTListener(TListener el) throws TooManyListeners; • For one listener • public void removeTListener(TListener el);

  21. These methods are used by event listeners to register an interest in events of a specific type. • The first pattern indicates that a Bean can multicast an event to multiple listeners. • The second pattern indicates that a Bean can unicast an event to only one listener. • The third pattern is used by a listener when it no longer wishes to receive a specific type of event notification from a Bean.

  22. Reflection • It is the ability to obtain information about the fields, constructors and methods of any class at run time. • Introspection uses reflection to obtain information about a bean. • JVM creates an instance of the class “Class” for each type including classes, interfaces, arrays and simple types. • Class provides various instance methods to get information about the type. • “Member” is an interface in java.lang.reflect that defines methods that are common to the fields, constructors and methods of a class.

  23. “Field” class in java.lang.reflect package contains methods to read and write the value of a field within another object. • This class implements Member interface. • The class Constructor in java.lang.reflect contains methods to get information about a constructor. It implements Member interface.

  24. The class Method in java.lang.reflect is used to get information about a method. It implements Member interface. • The class Array in java.lang.reflect is used to built an array at run time.

  25. Introspection • It enables to find out information about the structure and functionality of the bean. • Introspection services are provided by JavaBeans API are divided in- • Low-level API • Used by application builder tools. • Reflection work at low-level. • High-level API • Used by developers. • Limited access of a bean’s internal. • Introspection works at high-level.

  26. Introspector • This class in java.beans provides static methods to obtain information about the properties, events and methods of a bean. • When a bean named Abc is encountered, the introspection looks for a class AbcBeanInfo. If it exists, it is used to provide information about the properties, events and methods of component. If it doesn’t exist, a BeanInfo object is constructed by default. • getBeanInfo() encapsulates all information about a bean and wraps it up into a single BeanInfo object. • static BeanInfo getBeanInfo(Class beanClass) • static BeanInfo getBeanInfo(Class beanClass, Class ignoreClass)

  27. Developing a simple bean • Create a directory for new bean c:\\bdk\\demo\\sunw\\demo\\colors • Create java source file Colors.java • Compile java file • Create a Manifest File • switch to the c:\\bdk\\demo directory. • This is the directory in which the manifest files for the BDK demos are located. • Put the source code for your manifest file in the file colors.mft. • It is shown here: Name: sunw/demo/colors/Colors.class Java-Bean: True • This file indicates that there is one .class file in the JAR file and that it is a Java Bean. • Colors.class file is in the package sunw.demo.colors and in the subdirectory sunw\\demo\\colors relative to the current directory.

  28. Generate JAR file • Beans are included in the ToolBox window of the BDK only if they are in JAR files in the directory c:\\bdk\\jars. • These files are generated with the jar utility. Enter the following: • jar cfm colors.jar colors.mft sunw\\demo\\colors\\*.class • This command creates the file colors.jar and places it in the directory c:\\bdk\\jars. • Start BDK • Test.

  29. Example C:\beans\demo\sunw\demo\my\> javac My.java C:\beans\demo\> jar cfm ..\jars\my.jar My.mf sunw\demo\my\*.class

  30. BeanInfo interface • PropertyDescriptor[] getPropertyDescriptors() • EventSetDescriptor[] getEventSetDescriptors() • MethodDescriptor[] getMethodDescriptors()

  31. Constrained properties • A bean has constrained property generates an event when an attempt is made to change its value. • The event is of type PropertyChangeEvent.

More Related