SlideShare una empresa de Scribd logo
1 de 39
JavaBeans
ByBy
Shravan Kumar UpadhayayShravan Kumar Upadhayay
Department of Computer Science &Department of Computer Science &
EngineeringEngineering
Punjab Institute of Technology, KapurthalaPunjab Institute of Technology, Kapurthala
(Punjab Technical University Main Campus)(Punjab Technical University Main Campus)
 Why Beans?
 The Bean-Writing Process
 Using Beans to Build an Application
 Naming Patterns for Bean Properties and Events
 Bean Property Types
 Adding Custom Bean Events
 Property Editors
 Going Beyond Naming Patterns
 Customizers
 The Bean Context
BeanBean
 "A bean is a reusable software component based
on Sun's JavaBeans specification that can be
manipulated visually in a builder tool.“
 The JavaBeans technology enables vendors to
create environments that make it dramatically
easier to develop user interfaces for Java
applications.
 Once you implement a bean, others can use it in
a builder environment such as Forte, JBuilder,
VisualAge, or Visual Café to produce
applications or applets more efficiently.
VBVB
 You build the interface by dropping components
(called controls in Visual Basic) onto a form window.
 Through property sheets, you set properties of the
components such as height, color, or other behavior.
 The property sheets also list the events to which
components can react. For some of those events, you
write short snippets of event handling code.
VB & BeanVB & Bean
1. Beans with the same functionality as the most
common Visual Basic controls are readily
available.
2. Java technology builder tools have come much
closer to the ease of use of the Visual Basic
environment.
 Once you have built a bean, users of any
environment enabled for JavaBeans technology
can readily use it.
 First, clear that writing a bean is not technically
difficult—there are only a few new classes and
interfaces for you to master.
 In particular, the simplest kind of bean is really
nothing more than a Java platform class that
follows some fairly strict naming conventions
for its event listeners and its methods.
The Bean-Writing Process
 All accessor methods begin with get, all setter
methods begin with set.
 builder tools use this standard naming
convention to discover properties.
 Properties are conceptually at a higher level than
instance variables—they are features of the
interface, whereas instance variables belong to
the implementation of the class.
 The core classes and interfaces in the packagesThe core classes and interfaces in the packages
java.beans and java.beans.beancontext providejava.beans and java.beans.beancontext provide
the support for creating java beans.the support for creating java beans.
 There is no JavaBean super class that all javaThere is no JavaBean super class that all java
beans extend and no interface that all java beansbeans extend and no interface that all java beans
implement.implement.
 Creating a class of java beans largely involvesCreating a class of java beans largely involves
adhering to the standards of the java beanadhering to the standards of the java bean
component model.component model.
Elements of Java Bean InterfaceElements of Java Bean Interface
 Methods: A method represents some action that can beMethods: A method represents some action that can be
executed against the JavaBean. Example: a java beanexecuted against the JavaBean. Example: a java bean
that containsan animation may have method to startthat containsan animation may have method to start
and stop the animation.and stop the animation.
 Properties: A property represents an attribute of javaProperties: A property represents an attribute of java
bean, such as its color or font. The property does notbean, such as its color or font. The property does not
have to be a visible attribute. For example: a propertyhave to be a visible attribute. For example: a property
can be an abstract quality, such as a boolean flag thatcan be an abstract quality, such as a boolean flag that
indicates whether a component is enables for input.indicates whether a component is enables for input.
 Events: java bean objects use events to notifyEvents: java bean objects use events to notify
other java bean objects that some event hasother java bean objects that some event has
occurred. These objects use the same event-occurred. These objects use the same event-
handling mechanism as Swing and AWThandling mechanism as Swing and AWT
components.components.
 Java beans that must be aware of a certain event registerJava beans that must be aware of a certain event register
as a listener with java bean that generates the event.as a listener with java bean that generates the event.
 Listener java beans must implement the interface thatListener java beans must implement the interface that
corresponds to the event class of interest.corresponds to the event class of interest.
 Source java beans provides registration methods for theSource java beans provides registration methods for the
event. When the event occurs, the source java beanevent. When the event occurs, the source java bean
sends a copy of the event to each registered listener.sends a copy of the event to each registered listener.
Many of the events generated by beans areMany of the events generated by beans are
PropertyChangeEvent objects, but you can definePropertyChangeEvent objects, but you can define
custom events.custom events.
1. Beans need to be usable by less-than-expert
programmers. Such people will access most of the
functionality of your bean with a visual design tool that
uses essentially no programming. You need to expose
lots of properties to make it easy to customize the bean
behavior.
2. The same bean needs to be usable in a wide variety of
contexts. A bean that is too simplistic to be usable in the
real world won't get much respect. Of course, that is
why people can charge hundreds of dollars for a
professional component, such as a full-featured chart
control.
 If you are writing a program for a single application,
then you will simply choose one behavior or the other.
But if you are building a bean, you aren't programming
for yourself but for a wide audience of users with
different needs.
 Therefore, it would be best to add a property called
something like incrementalUpdate and implement both
behaviors, depending on the value of that property.
 Fortunately, you need to master only a small
number of concepts to write beans with a rich
set of behaviors.
Main characteristicsMain characteristics
 If a bean has a property named X, it can haveIf a bean has a property named X, it can have
public methods named setX and getX, to assignpublic methods named setX and getX, to assign
and return the value of the property X.and return the value of the property X.
 For boolean type properties methods are setXFor boolean type properties methods are setX
and isX.and isX.
 All beans should have a constructor that takesAll beans should have a constructor that takes
no arguments because most beanboxes call thisno arguments because most beanboxes call this
constructor.constructor.
Using Beans to Build an Application
 In particular, the only way to use Java Beans in
an ordinary program in the Java programming
language would be to write code that constructs
an object of the bean class, places the object into
a container, and calls the setProperty method.
Packaging Beans in JAR files
 To make any bean usable in a builder tool,
package all class files that are used by the bean
code into a JAR file.
 Unlike the JAR files for an applet that you saw
previously, a JAR file for a bean needs a
manifest file that specifies which class files in the
archive are beans and should be included in the
Toolbox.
Naming Patterns for Bean Properties
and Events
 First, clear that there is no cosmic beans class that you
extend to build your beans. Visual beans directly or
indirectly extend the Component class, but no-nvisual
beans don't have to extend any particular superclass.
 A bean is simply any class that can be manipulated in a
builder tool. The builder tool does not look at the
superclass to determine the bean nature of a class, but it
analyzes the names of its methods. To enable this
analysis, the method names for beans must follow
certain patterns.
 The naming pattern for properties is simple: AnyThe naming pattern for properties is simple: Any
pair of methodspair of methods
 public X getPropertyName()public X getPropertyName()
 public void setPropertyName(X x)public void setPropertyName(X x)
 corresponds to a read/write property of type X.corresponds to a read/write property of type X.
Bean Property Types
 A sophisticated bean will have lots of different
kinds of properties that it should expose in a
builder tool for a user to set at design time or get
at run time.
 It can also trigger both standard and custom
events.
 Getting the properties of your beans right is
probably the most complex part of building a
bean because the model is quite rich. The
JavaBeans specification allows four types of
properties.
Simple Properties
 A simple property is one that takes a single value
such as a string or a number.
 Simple properties are easy to program: just use
the set/get naming convention we indicated
earlier.
public void setFileName(String f)
{
fileName = f;
image = . . .
repaint();
}
public String getFileName()
{
if (file == null) return null;
else return file.getPath();
}
Indexed Properties
 An indexed property is one that gets or sets an
array. With an indexed property, you supply two
pairs of get and set methods: one for the array
and one for individual entries.
 They must follow the pattern:
 X[] getPropertyName()
 void setPropertyName(X[] x)
 X getPropertyName(int i)
 void setPropertyName(int i, X x)
Bound Properties
 Bound properties tell interested listeners that
their value has changed.
 To implement a bound property, you must implement twoTo implement a bound property, you must implement two
mechanisms.mechanisms.
 Whenever the value of the property changes, the bean must sendWhenever the value of the property changes, the bean must send
a PropertyChange event to all registered listeners. This changea PropertyChange event to all registered listeners. This change
can occur when the set method is called or when the programcan occur when the set method is called or when the program
user carries out an action, such as editing text or selecting a file.user carries out an action, such as editing text or selecting a file.
 To enable interested listeners to register themselves, the bean hasTo enable interested listeners to register themselves, the bean has
to implement the following two methods:to implement the following two methods:
 Void addPropertyChangeListener(PropertyChangeListener listener)Void addPropertyChangeListener(PropertyChangeListener listener)
 Void removePropertyChangeListener(PropertyChangeListener listener)Void removePropertyChangeListener(PropertyChangeListener listener)
 The java.beans package has a convenience class,
called PropertyChangeSupport, that manages the
listeners for you.
 To use this convenience class, your bean must
have a data field of this class that looks like this:
 private PropertyChangeSupport changeSupport =
new PropertyChangeSupport(this);
 You delegate the task of adding and removing
property change listeners to that object.
public void
addPropertyChangeListener(PropertyChangeListener listener)
{
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener)
{
changeSupport.removePropertyChangeListener(listener
);
}
Constrained PropertiesConstrained Properties
 A constrained property is the most interesting ofA constrained property is the most interesting of
the properties, and also the most complex tothe properties, and also the most complex to
implement. Such a property is constrained byimplement. Such a property is constrained by
the fact that any listener can "veto" proposedthe fact that any listener can "veto" proposed
changes, forcing it to revert to the old setting.changes, forcing it to revert to the old setting.
Adding Custom Bean Events
 When you add a bound or constrained property to a
bean, you also enable the bean to fire events whenever
the value of that property changes.
 There are events that a bean can send out, for example,
 When the program user has clicked on a control within
the bean;
 When new information is available;
 Or, simply when some amount of time has elapsed.
 Write a class CustomEvent that extends EventObject.Write a class CustomEvent that extends EventObject.
(The event name must end in Event in order for a(The event name must end in Event in order for a
builder to use the naming patterns to find it.)builder to use the naming patterns to find it.)
 Write an interface CustomListener with one or moreWrite an interface CustomListener with one or more
notification methods. Those methods can have anynotification methods. Those methods can have any
name, but they must have a single parameter of typename, but they must have a single parameter of type
CustomEvent and return type void.CustomEvent and return type void.
 Supply the following two methods in the bean:Supply the following two methods in the bean:
 public void addCustomListener(CustomListener e)public void addCustomListener(CustomListener e)
 public void removeCustomListener(CustomListener e)public void removeCustomListener(CustomListener e)
Property Editors
 If you add an integer or string property to a bean, then that
property is automatically displayed in the bean's property
inspector.
 if you add a property whose values cannot easily be edited in a
text field, for example, a date or a Color? Then, you need to
provide a separate component that the user can use to specify
the property value.
 Such components are called property editors.
 For example, a property editor for a date object might be a
calendar that lets the user scroll through the months and pick a
date. A property editor for a Color object would let the user
select the red, green, and blue components of the color.
Customizers
 A property editor, no matter how sophisticated, is
responsible for allowing the user to set one property at
a time.
 Especially if certain properties of a bean relate to each
other, it may be more user friendly to give users a way
to edit multiple properties at the same time.
 To enable this feature, you supply a customizer instead of
(or in addition to) multiple property editors.
introduction of Java beans

Más contenido relacionado

La actualidad más candente

Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 

La actualidad más candente (20)

Features of java
Features of javaFeatures of java
Features of java
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Java beans
Java beansJava beans
Java beans
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
EJB3 Basics
EJB3 BasicsEJB3 Basics
EJB3 Basics
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java program structure
Java program structureJava program structure
Java program structure
 
Web controls
Web controlsWeb controls
Web controls
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 

Destacado

EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
Stephan Janssen
 
Santa Ana School.pptx
Santa Ana School.pptxSanta Ana School.pptx
Santa Ana School.pptx
sofiathoma
 
Component object model and
Component object model andComponent object model and
Component object model and
Saransh Garg
 
Dcom vs. corba
Dcom vs. corbaDcom vs. corba
Dcom vs. corba
Mohd Arif
 

Destacado (20)

Javabeans
JavabeansJavabeans
Javabeans
 
Java beans
Java beansJava beans
Java beans
 
Java beans
Java beansJava beans
Java beans
 
Beans presentation
Beans presentationBeans presentation
Beans presentation
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
Santa Ana School.pptx
Santa Ana School.pptxSanta Ana School.pptx
Santa Ana School.pptx
 
EJB3 Advance Features
EJB3 Advance FeaturesEJB3 Advance Features
EJB3 Advance Features
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
javabeans
javabeansjavabeans
javabeans
 
Unit iv
Unit ivUnit iv
Unit iv
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java beans
Java beansJava beans
Java beans
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
 
Java EE EJB Applications
Java EE EJB ApplicationsJava EE EJB Applications
Java EE EJB Applications
 
Java Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIJava Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUI
 
Java beans
Java beansJava beans
Java beans
 
Using java beans(ii)
Using java beans(ii)Using java beans(ii)
Using java beans(ii)
 
Component object model and
Component object model andComponent object model and
Component object model and
 
Presentation On Com Dcom
Presentation On Com DcomPresentation On Com Dcom
Presentation On Com Dcom
 
Dcom vs. corba
Dcom vs. corbaDcom vs. corba
Dcom vs. corba
 

Similar a introduction of Java beans

Spring bean mod02
Spring bean mod02Spring bean mod02
Spring bean mod02
Guo Albert
 
Spring framework
Spring frameworkSpring framework
Spring framework
Ajit Koti
 
Rmi, corba and java beans
Rmi, corba and java beansRmi, corba and java beans
Rmi, corba and java beans
Raghu nath
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
CommercialSystemsBahman.ppt
CommercialSystemsBahman.pptCommercialSystemsBahman.ppt
CommercialSystemsBahman.ppt
KalsoomTahir2
 

Similar a introduction of Java beans (20)

Javabeans .pdf
Javabeans .pdfJavabeans .pdf
Javabeans .pdf
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
 
Java beans
Java beansJava beans
Java beans
 
Java Beans Unit 4(part 2)
Java Beans Unit 4(part 2)Java Beans Unit 4(part 2)
Java Beans Unit 4(part 2)
 
Java beans
Java beansJava beans
Java beans
 
How to use the java bean api library
How to use the java bean api libraryHow to use the java bean api library
How to use the java bean api library
 
How to use the java bean api library
How to use the java bean api libraryHow to use the java bean api library
How to use the java bean api library
 
P20CSP105-AdvJavaProg.pptx
P20CSP105-AdvJavaProg.pptxP20CSP105-AdvJavaProg.pptx
P20CSP105-AdvJavaProg.pptx
 
Spring bean mod02
Spring bean mod02Spring bean mod02
Spring bean mod02
 
Jfxpub binding
Jfxpub bindingJfxpub binding
Jfxpub binding
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Universal Java Beans with DB2 from 1999, early Internet work
Universal Java Beans with DB2 from 1999, early Internet workUniversal Java Beans with DB2 from 1999, early Internet work
Universal Java Beans with DB2 from 1999, early Internet work
 
Rmi, corba and java beans
Rmi, corba and java beansRmi, corba and java beans
Rmi, corba and java beans
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
Javabean1
Javabean1Javabean1
Javabean1
 
CommercialSystemsBahman.ppt
CommercialSystemsBahman.pptCommercialSystemsBahman.ppt
CommercialSystemsBahman.ppt
 

Último

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

introduction of Java beans

  • 1. JavaBeans ByBy Shravan Kumar UpadhayayShravan Kumar Upadhayay Department of Computer Science &Department of Computer Science & EngineeringEngineering Punjab Institute of Technology, KapurthalaPunjab Institute of Technology, Kapurthala (Punjab Technical University Main Campus)(Punjab Technical University Main Campus)
  • 2.  Why Beans?  The Bean-Writing Process  Using Beans to Build an Application  Naming Patterns for Bean Properties and Events  Bean Property Types  Adding Custom Bean Events  Property Editors  Going Beyond Naming Patterns  Customizers  The Bean Context
  • 3. BeanBean  "A bean is a reusable software component based on Sun's JavaBeans specification that can be manipulated visually in a builder tool.“  The JavaBeans technology enables vendors to create environments that make it dramatically easier to develop user interfaces for Java applications.
  • 4.  Once you implement a bean, others can use it in a builder environment such as Forte, JBuilder, VisualAge, or Visual Café to produce applications or applets more efficiently.
  • 5. VBVB  You build the interface by dropping components (called controls in Visual Basic) onto a form window.  Through property sheets, you set properties of the components such as height, color, or other behavior.  The property sheets also list the events to which components can react. For some of those events, you write short snippets of event handling code.
  • 6. VB & BeanVB & Bean 1. Beans with the same functionality as the most common Visual Basic controls are readily available. 2. Java technology builder tools have come much closer to the ease of use of the Visual Basic environment.
  • 7.  Once you have built a bean, users of any environment enabled for JavaBeans technology can readily use it.
  • 8.  First, clear that writing a bean is not technically difficult—there are only a few new classes and interfaces for you to master.  In particular, the simplest kind of bean is really nothing more than a Java platform class that follows some fairly strict naming conventions for its event listeners and its methods.
  • 9. The Bean-Writing Process  All accessor methods begin with get, all setter methods begin with set.  builder tools use this standard naming convention to discover properties.  Properties are conceptually at a higher level than instance variables—they are features of the interface, whereas instance variables belong to the implementation of the class.
  • 10.  The core classes and interfaces in the packagesThe core classes and interfaces in the packages java.beans and java.beans.beancontext providejava.beans and java.beans.beancontext provide the support for creating java beans.the support for creating java beans.  There is no JavaBean super class that all javaThere is no JavaBean super class that all java beans extend and no interface that all java beansbeans extend and no interface that all java beans implement.implement.  Creating a class of java beans largely involvesCreating a class of java beans largely involves adhering to the standards of the java beanadhering to the standards of the java bean component model.component model.
  • 11. Elements of Java Bean InterfaceElements of Java Bean Interface  Methods: A method represents some action that can beMethods: A method represents some action that can be executed against the JavaBean. Example: a java beanexecuted against the JavaBean. Example: a java bean that containsan animation may have method to startthat containsan animation may have method to start and stop the animation.and stop the animation.  Properties: A property represents an attribute of javaProperties: A property represents an attribute of java bean, such as its color or font. The property does notbean, such as its color or font. The property does not have to be a visible attribute. For example: a propertyhave to be a visible attribute. For example: a property can be an abstract quality, such as a boolean flag thatcan be an abstract quality, such as a boolean flag that indicates whether a component is enables for input.indicates whether a component is enables for input.
  • 12.  Events: java bean objects use events to notifyEvents: java bean objects use events to notify other java bean objects that some event hasother java bean objects that some event has occurred. These objects use the same event-occurred. These objects use the same event- handling mechanism as Swing and AWThandling mechanism as Swing and AWT components.components.
  • 13.  Java beans that must be aware of a certain event registerJava beans that must be aware of a certain event register as a listener with java bean that generates the event.as a listener with java bean that generates the event.  Listener java beans must implement the interface thatListener java beans must implement the interface that corresponds to the event class of interest.corresponds to the event class of interest.  Source java beans provides registration methods for theSource java beans provides registration methods for the event. When the event occurs, the source java beanevent. When the event occurs, the source java bean sends a copy of the event to each registered listener.sends a copy of the event to each registered listener. Many of the events generated by beans areMany of the events generated by beans are PropertyChangeEvent objects, but you can definePropertyChangeEvent objects, but you can define custom events.custom events.
  • 14. 1. Beans need to be usable by less-than-expert programmers. Such people will access most of the functionality of your bean with a visual design tool that uses essentially no programming. You need to expose lots of properties to make it easy to customize the bean behavior. 2. The same bean needs to be usable in a wide variety of contexts. A bean that is too simplistic to be usable in the real world won't get much respect. Of course, that is why people can charge hundreds of dollars for a professional component, such as a full-featured chart control.
  • 15.  If you are writing a program for a single application, then you will simply choose one behavior or the other. But if you are building a bean, you aren't programming for yourself but for a wide audience of users with different needs.  Therefore, it would be best to add a property called something like incrementalUpdate and implement both behaviors, depending on the value of that property.
  • 16.  Fortunately, you need to master only a small number of concepts to write beans with a rich set of behaviors.
  • 17. Main characteristicsMain characteristics  If a bean has a property named X, it can haveIf a bean has a property named X, it can have public methods named setX and getX, to assignpublic methods named setX and getX, to assign and return the value of the property X.and return the value of the property X.  For boolean type properties methods are setXFor boolean type properties methods are setX and isX.and isX.
  • 18.  All beans should have a constructor that takesAll beans should have a constructor that takes no arguments because most beanboxes call thisno arguments because most beanboxes call this constructor.constructor.
  • 19. Using Beans to Build an Application  In particular, the only way to use Java Beans in an ordinary program in the Java programming language would be to write code that constructs an object of the bean class, places the object into a container, and calls the setProperty method.
  • 20. Packaging Beans in JAR files  To make any bean usable in a builder tool, package all class files that are used by the bean code into a JAR file.  Unlike the JAR files for an applet that you saw previously, a JAR file for a bean needs a manifest file that specifies which class files in the archive are beans and should be included in the Toolbox.
  • 21. Naming Patterns for Bean Properties and Events  First, clear that there is no cosmic beans class that you extend to build your beans. Visual beans directly or indirectly extend the Component class, but no-nvisual beans don't have to extend any particular superclass.  A bean is simply any class that can be manipulated in a builder tool. The builder tool does not look at the superclass to determine the bean nature of a class, but it analyzes the names of its methods. To enable this analysis, the method names for beans must follow certain patterns.
  • 22.  The naming pattern for properties is simple: AnyThe naming pattern for properties is simple: Any pair of methodspair of methods  public X getPropertyName()public X getPropertyName()  public void setPropertyName(X x)public void setPropertyName(X x)  corresponds to a read/write property of type X.corresponds to a read/write property of type X.
  • 23. Bean Property Types  A sophisticated bean will have lots of different kinds of properties that it should expose in a builder tool for a user to set at design time or get at run time.  It can also trigger both standard and custom events.
  • 24.  Getting the properties of your beans right is probably the most complex part of building a bean because the model is quite rich. The JavaBeans specification allows four types of properties.
  • 25. Simple Properties  A simple property is one that takes a single value such as a string or a number.  Simple properties are easy to program: just use the set/get naming convention we indicated earlier.
  • 26. public void setFileName(String f) { fileName = f; image = . . . repaint(); } public String getFileName() { if (file == null) return null; else return file.getPath(); }
  • 27. Indexed Properties  An indexed property is one that gets or sets an array. With an indexed property, you supply two pairs of get and set methods: one for the array and one for individual entries.
  • 28.  They must follow the pattern:  X[] getPropertyName()  void setPropertyName(X[] x)  X getPropertyName(int i)  void setPropertyName(int i, X x)
  • 29. Bound Properties  Bound properties tell interested listeners that their value has changed.
  • 30.  To implement a bound property, you must implement twoTo implement a bound property, you must implement two mechanisms.mechanisms.  Whenever the value of the property changes, the bean must sendWhenever the value of the property changes, the bean must send a PropertyChange event to all registered listeners. This changea PropertyChange event to all registered listeners. This change can occur when the set method is called or when the programcan occur when the set method is called or when the program user carries out an action, such as editing text or selecting a file.user carries out an action, such as editing text or selecting a file.  To enable interested listeners to register themselves, the bean hasTo enable interested listeners to register themselves, the bean has to implement the following two methods:to implement the following two methods:  Void addPropertyChangeListener(PropertyChangeListener listener)Void addPropertyChangeListener(PropertyChangeListener listener)  Void removePropertyChangeListener(PropertyChangeListener listener)Void removePropertyChangeListener(PropertyChangeListener listener)
  • 31.  The java.beans package has a convenience class, called PropertyChangeSupport, that manages the listeners for you.
  • 32.  To use this convenience class, your bean must have a data field of this class that looks like this:  private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);  You delegate the task of adding and removing property change listeners to that object.
  • 33. public void addPropertyChangeListener(PropertyChangeListener listener) { changeSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { changeSupport.removePropertyChangeListener(listener ); }
  • 34. Constrained PropertiesConstrained Properties  A constrained property is the most interesting ofA constrained property is the most interesting of the properties, and also the most complex tothe properties, and also the most complex to implement. Such a property is constrained byimplement. Such a property is constrained by the fact that any listener can "veto" proposedthe fact that any listener can "veto" proposed changes, forcing it to revert to the old setting.changes, forcing it to revert to the old setting.
  • 35. Adding Custom Bean Events  When you add a bound or constrained property to a bean, you also enable the bean to fire events whenever the value of that property changes.  There are events that a bean can send out, for example,  When the program user has clicked on a control within the bean;  When new information is available;  Or, simply when some amount of time has elapsed.
  • 36.  Write a class CustomEvent that extends EventObject.Write a class CustomEvent that extends EventObject. (The event name must end in Event in order for a(The event name must end in Event in order for a builder to use the naming patterns to find it.)builder to use the naming patterns to find it.)  Write an interface CustomListener with one or moreWrite an interface CustomListener with one or more notification methods. Those methods can have anynotification methods. Those methods can have any name, but they must have a single parameter of typename, but they must have a single parameter of type CustomEvent and return type void.CustomEvent and return type void.  Supply the following two methods in the bean:Supply the following two methods in the bean:  public void addCustomListener(CustomListener e)public void addCustomListener(CustomListener e)  public void removeCustomListener(CustomListener e)public void removeCustomListener(CustomListener e)
  • 37. Property Editors  If you add an integer or string property to a bean, then that property is automatically displayed in the bean's property inspector.  if you add a property whose values cannot easily be edited in a text field, for example, a date or a Color? Then, you need to provide a separate component that the user can use to specify the property value.  Such components are called property editors.  For example, a property editor for a date object might be a calendar that lets the user scroll through the months and pick a date. A property editor for a Color object would let the user select the red, green, and blue components of the color.
  • 38. Customizers  A property editor, no matter how sophisticated, is responsible for allowing the user to set one property at a time.  Especially if certain properties of a bean relate to each other, it may be more user friendly to give users a way to edit multiple properties at the same time.  To enable this feature, you supply a customizer instead of (or in addition to) multiple property editors.