JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.

Slides:



Advertisements
Similar presentations
Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Advertisements

Component Oriented Programming 1 Chapter 2 Theory of Components.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.
By Dr. Jiang B. Liu 12. The Java Beans. Java Beans n JavaBeans is a portable, platform-independent software component model written in Java. It enables.
JavaBeans A component architecture. What is JavaBeans? NC World (New Computing) Dictionary: JavaBeans n. 1. JavaSoft technology. 2. Component object model.
JAVA BEANS By Madhuri Kakumanu. What is a Java Bean? “ A Java Bean is a reusable software component that can be visually manipulated in builder tools.”
ASP.NET Programming with C# and SQL Server First Edition
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
M.Sc. Course, Dept. of Informatics and Telecommunications, University of Athens S.Hadjiefthymiades “Web Application Servers” Basics on WAS WAS are necessary.
JAVA BEANS (Unit-V) K.Phani Sirisha.
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
Introducing JavaBeans Identify the features of a JavaBean Create and add a bean to an application Manipulate bean properties using accessor and mutator.
Java Beans.  Java offers software component development through java Beans  Java Beans are based on a software component model for java.  The model.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
Struts 2.0 an Overview ( )
Component-Based Software Engineering Introduction to Java Beans Paul Krause and Sotiris Moschoyiannis.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
Java Beans.
Enterprise Java Beans Part I Kyungmin Cho 2001/04/10.
Introduction to J2EE Architecture Portions by Kunal Mehta.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
J2EE Structure & Definitions Catie Welsh CSE 432
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Comparing JavaBeans and OSGi Towards an Integration of Two Complementary Component Models HUMBERTO CERVANTES JEAN-MARIE FAVRE 09/02.
CG0165: Advanced Applications Development in Java Michael Brockway Sajjad Shami Week 5: JavaBeans Northumbria University School of Computing, Engineering.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Introduction to Component-Based Engineering Howard Abrams
Components Components are specialized self contained Software entities that can be replicated, Customized and inserted into applications. Components come.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
Introduction to Java Beans CIS 421 Web-based Java Programming.
Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Chapter 5 Introduction to Defining Classes
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
Introduction to Java Beans From Anders Børjesson.
Introduction to Software Components: the JavaBeans specs Babak Esfandiari.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Beans THETOPPERSWAY.COM. Contents What is a Java Bean? Advantages Properties Bean Developers Kit (BDK) What makes Bean possible? Support for Java.
Java Beans - Basics CIS 421 Web-based Java Programming.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 32 JavaBeans and Bean Events
Structure of a web application
Chapter 36 JavaBeans and Bean Events
Javabeans for dummies.
JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine
Java Beans Sagun Dhakhwa.
JavaBeans and JSP CS-422.
Persistence & Bean Properties
Reusability 11/29/2018© 2006 ITT Educational Services Inc.
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Component-Based Software Engineering
Java Programming: Advanced Topics
Presentation transcript:

JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean is not distributed component like EJB Interface of javabean is provided by 1. Design pattern(implicitly) 2. Using a class to implement the BeanInfo or Customizer interface(explicitly)

Javabean Component It is a binary building block Development and deployment of a javabean Assembly javabeans to build a new javabean or a new application, applet Write glue codes to wire all beans together javabean with CORBA as a CORBA client Client side javabean Javabean for business logic process in MVC on server javabean on server is not visible

Advantage of Java Bean Write once, run anywhere The properties, events, and methods of a bean that are exposed to an application builder tool can be controlled They are the interface of the bean. They are platform independent Configuration setting of a bean can be saved in persistent storage and restored later Bean may register and receive events from other object and can generate event sent to other objects (Bean communication)

JavaBean Component BeanInfo Events Methods Properties JAR Customizer

Design Pattern All beans should implement the Serializable interface so that the state can be saved and later restored Methods must be made public All exposed methods should be threadsafe, possibly synchronized to prevent more than one thread from calling method at a given time Propertie X is exposed by public setX and getX methods Boolean property may be exposed by isX method which returns a boolean value The bean which may trigger event must provide addEventListener and removeEventListener mehods for other bean to register with it to be notified

Deployment of Bean All java classes can be converted to a bean Bean is compressed and saved in the format of jar file which contains manifest file, class files, gif files, and other information customization files Sun NetBeans, BDK, Visual Café, JBuilder, Visual Age are the bean builder tools

Criteria to be a bean Can this piece of code be used in more than one area? Can you quickly think of ways that this piece of code might be customized? Is the purpose of this code easy to explain? Does this code module contain all the info it needs to work itself? Does it have good encapsulation? If you answer all “yes”, You should make the class a bean

JAR file JAR file allows you to efficiently deploy a set of classes and their associated resources. JAR file makes it much easier to deliver, install, and download. It is compressed.

Manifest file Manifest.tmp Name: SimpleBean.class Java-Bean: True...

Creating and extract a jar file Create a jar file jar cfm simplebean.jar manifest.tmp *.class Extracting files from a jar file jar xf simplebean.jar

Develop a New Bean Create a directory for the new bean Create the java bean source file(s) Compile the source file(s) Create a manifest file Generate a JAR file Start BDK Test Working-dir can be at \demo where is the installation dir for BDK

Create bean source file - SimpleBean.java package simplebean; import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable { public SimpleBean(){ setSize(60,40); setBackground(Color.red);}}

Compile and make jar file Javac -d. SimpleBean.java Edit a manifest file called manifest.tmp Name: SimpleBean.class Java-Bean: True jar cfm..\jars\simplebean.jar manifest.tmp simplebean\*.class [SimpleBean and colorsbean demo]

Introspection Process of analyzing a bean to determine the capability Allows application builder tool to present info about a component to software designer Naming convention implicit method BeanInfo class to explicitly infer info of a bean

Design Pattern for Properties Property is a subset of a bean’s state which determines the appearance and behavior of the component Simple property Indexed Property Bound Property Constrained property

Simple Property Simple property has a single value. N is the name of the property and T is its type public T getN(); public void setN(T arg) For readonly property there is getN() method only

Indexed Property One property may consists of multiple values stored in an array public T getN(int index); public void setN(int index, T value); public T[] getN(); public void setN(T values[]); where N may be a double data[] and T is double

Bound Property It can generate an event when the property is changed The event is of type PropertyChangeEvent and is sent to objects that previously registered an interest in receiving such notifications bean with bound property - Event source Bean implementing listener -- event target

Implement Bound Property in a Bean 1. Import java.beans package 2. Instantiate a PropertyChangeSupport object private PropertyChangeSupport changes = new PropertyChangeSupport(this); 3. Implement methods to maintain the property change listener list: public void addPropertyChangeListener(PropertyChangeListener l) { changes.addPropertyChangeListener(l);} also removePropertyChangeListener method is needed

Event Source Cont. 4. Modify a property’s setter method to fire a property change event when the property is changed. Public void setX(int newX){ int oldx = x; x = newX; changes.firePropertyChange(“x”, oldX, newX);}

Implement Bound Property Listener 1. Listener bean must implement PropertyChangeListner interface public class MyClass implements PropertyChangeListener, Serializable 2. It must override this method: public abstract void propertyChange(PropertyChangeevent evt)

Registration For example: OurButton button = new OurButton(); button.addPropertyChangeListener(aButtonListener); …..

Constrained Property It generates an event when an attempt is made to change it value The event type is PropertyChangeEvent The event is sent to objects that previously registered an interest in receiving an such notification Those other objects have the ability to veto the proposed change This allows a bean to operate differently according to the runtime environment

Three Parts in Implementation of Constrained Property 1. Source bean containing one or more constrained properties 2. Listener objects that implement the VetoableChangeListener interface. This object either accepts or rejects the proposed change. 3. PropertyChangeEvent object containing property name, old value, new value.

Implement Constrained Property in a Bean Bean with constrained property must 1. Allow VetoableChangeListener object to register and unregister its interest in receiving notifications 2. Fire property change at those registered listeners. The event is fired before the actual property change takes place

Implementation of Constrained Property in a Bean 1. Import java.beans package 2. Instantiate a VetoableChangeSupport object: private VetoableChangeSupport vetos=new VetoableChangeSupport(this); 3. Implement methods to maintain the property change listener list: public void addVetoableChangelistener(VetoableChangelistener l) { vetos.addVetoableChangeListener(l);}

Cont. 4. Write a property’s setter method to fire a property change event: public void setX(int newX) { int oldX=X; vetos.fireVetoableChange(“X”, oldX, newX); //if no veto there X=newX; changes.firePropertyChange(“X”, oldX, newX); }

Implementing Constrained Property Listeners 1. Implements the VetoableChangeListener interface which has an abstract method Void vetoChange(PropertyChangeEvent evt) 2. Overide this abstract method. This is the method that will be called by the source bean on each object in the listener list kept by vetoableChangeSupport object

Persistence It has the ability to save a bean to storage and retrieve it at a later time Configuration settings are saved It is implemented by Java serialization If a bean inherits directly or indirectly from Component class it is automatically Serializable. Transient keyword can be used to designate data members not be saved ex. Thread reference member

Customizers Property sheet may not be the best user interface for a complex component It can provide step-by-step wizard guide to use component It can provide a GUI frame with image which visually tells what is changed such as radio button, check box,... It can customize the appearance and behavior of the properties

Design Pattern for User Defined Events Bean can generate events and send them to other objects in delegation event model Listener registration and unregistration public void addTListener(TListener x); public void removeTListener(TListener x);

User defined listener and event Timer class will fire off timeout event: public interface TimerListener extends EventListener{ public void timeOut(TimeEvent e);} public class TimerEvent extends EventObject{ int count=0; public TimerEvent(Object obj){super(obj);} public int getCount(){ return count;} public void setCount(int count){this.count=count;}}

Timer class fires off timeout event to all registered objects public void startTimer(){ if (t==null){t=new Thread(this); t.start();} public void run(){for(;;){ try{t.sleep(timeout);} catch(Exception e){} fireOff();}} void fireOff(){TimerEvent te=new TimeEvent(this); te.setCount(number); Vector listeners = (Vector)listeners.clone(); for(int I=0;I<listeners.size(); I++){ ((TimerListener)listeners.elementAt(I).timeOut(te);}

Any class that implements TimerListener can register itself by calling addTimerListener() Vector listeners = new Vector(); public void addTimerListener(TimerListener l){ listeners.addElement(l);} public void removeTimerListener(TimerListener l){ listeners.removeElement(l);} Any class that implements TimerListener interface must override the timeOut method of interface TimerListener to respond the notification

Colors.java package colors; import java.awt.*; import java.awt.event.*; public class Colors extends Canvas{ transient private Color color; private boolean rect; public Colors(){ addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent me){change();}}); rect=false; setSize(200,100); change();}

Colors.java(Cont.) public boolean getRect(){ return rect;} public void setRect(boolean flag){this.rect=flag; repaint();} public void change(){color=randomColor(); repaint();} private Color randomColor(){ int r=(int)(255*Math.random()); int g =(int)(255*Math.random()); int b=(int)(255*Math.random()); return new Color(r,g,b);} public void paint(Graphics g){ Dimension d = getSize(); int h=d.height; int w=d.width; g.setColor(color); if(rect){g.fillRect(0,0,w-1,h-1);} else{g.fillOval(0,0,w-1,h-1);}}}

Summary JavaBean is a platform-neutral component architecture for reusable software component It is a black box component to be used to build large component or application Property,method,event, introspector, customizer are parts of javabean interface

Quiz True of False 1. One JavaBean can fire off an event taken by more than one targets. 2. Javabean can’t be inherited by other bean. 3. Javabean can be distributed. 4. Javabean is in binary format and deployed in JAR file 5. Javabean can only be introspected by property sheet. 6. Javabean is a language independent architecture

Quiz Javabean can only be developed by BDK. Every java class can be turned into a Javabean. Javabean must be a graphic component. Javabean can be used on web server. The event a Javabean can trigger is a subclass of class EventObject The bound property bean can prevent its property change by other bean. JAR file is compressed file

Quiz Event source must implement listener and override the method provided by the listener Event Target must provide the addTListener and removeTListener methods BeanInfo class is used to explicitly introspect the bean property Customizer class can be used to provide winzard for bean configuration