SlideShare a Scribd company logo
1 of 27
Download to read offline
Advanced Java Programming
Topic: Java Beans

By
Ravi Kant Sahu
Asst. Professor, LPU
Introduction


Every Java user interface class is a JavaBeans component.



Java Beans makes it easy to reuse software components.



Developers can use software components written by others
without having to understand their inner workings.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Beans


JavaBeans is a software component architecture that extends the
power of the Java language by enabling well-formed objects to
be manipulated visually at design time in a pure Java builder
tool.



It is a reusable software component.



A JavaBean is a specially constructed Java class written in the
Java and coded according to the JavaBeans API specifications.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Beans Specifications
1.
2.

3.

4.

5.

A bean must be a public class.
A bean must have a public no-arg constructor, though it can have
other constructors if needed.
public MyBean();
A bean must implement the Serializable interface to ensure a
persistent state.
It should provide methods to set and get the values of the
properties, known as getter (accessor) and setter (mutator)
methods.
A bean may have events with correctly constructed public
registration and deregistration methods that enable it to add and
remove listeners.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Beans Specification


The first three requirements must be observed, and therefore
are referred to as minimum JavaBeans component
requirements.



The last two requirements depend on implementations.



It is possible to write a bean component without get/set
methods and event registration/deregistration methods.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Beans Example
public class StudentsBean implements java.io.Serializable
{ private String firstName = null;
private String lastName = null;
private int age = 0;
public StudentsBean() { }
public String getFirstName() { return firstName; }
public String getLastName(){ return lastName; }
public int getAge(){ return age; }
public void setFirstName(String firstName)
{ this.firstName = firstName; }
public void setLastName(String lastName)
{ this.lastName = lastName; }
public void setAge(Integer age){ this.age = age; } }
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Beans Component


The classes that define the beans, referred to as JavaBeans
components or bean components.



A JavaBeans component is a serializable public class with a
public no-arg constructor.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bean Properties


Properties are discrete, named attributes of a Java bean that can
affect its appearance or behavior.



They are often data fields of a bean.
For example, JButton component has a property named text.





Accessor and mutator methods are provided to let the user read
and write the properties.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Property-Naming Patterns


The bean property-naming pattern is a convention of the
JavaBeans component model that simplifies the bean developer's
task of presenting properties.



A property can be a primitive data type or an object type.



The property type dictates the signature of the accessor and
mutator methods.

Note: Properties describe the state of the bean. Naturally, data
fields are used to store properties.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Accessor Methods


The accessor method is named get<PropertyName>(), which
takes no parameters and returns a primitive type value or an
object of a type identical to the property type.



public String getMessage()
public int getXCoordinate()
public int getYCoordinate()




Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)


For a property of boolean type, the accessor method should be
named
is<PropertyName>( )

which returns a boolean value.
For example:
public boolean isCentered()

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Mutator Methods


The mutator method should be named as
set<PropertyName>(dataType p)
which takes a single parameter identical to the property type and
returns void.






public void setMessage(String s)
public void setXCoordinate(int x)
public void setYCoordinate(int y)
public void setCentered(boolean centered)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java EvEnt ModEl

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Event Delegation Model


The Java event delegation model provides the foundation for
beans to send, receive, and handle events.



The Java event model consists of the following three types of
elements:
The event object: An event object contains the information that



describes the event.


The source object: A source object is where the event originates.
When an event occurs on a source object, an event object is created.



The event listener object: An object interested in the event handles
the event. Such an object is called a listener.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Event classes and Event Listener interfaces


An event object is created using an event class, such as
ActionEvent, MouseEvent, and ItemEvent.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Source Components


The source component contains the code that detects an
external or internal action that triggers the event.



Upon detecting the action, the source should fire an event
to the listeners by invoking the event handler defined by
the listeners.



The source component must also contain methods for
registering and deregistering listeners.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Listener Components


A listener component for an event must implement the event
listener interface.



The object of the listener component cannot receive event
notifications from a source component unless the object is
registered as a listener of the source.



A listener component may implement any number of listener
interfaces to listen to several types of events.



A source component may register many listeners. A source
component may register itself as a listener.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating Custom Source Components


A source component must have the appropriate
registration and deregistration methods for adding and
removing listeners.



Events can be unicasted (only one listener object is
notified of the event) or multicasted (each object in a
list of listeners is notified of the event).

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Unicast
public void add<Event>Listener(<Event>Listener l)
throws TooManyListenersException;

Multicast
The naming pattern for adding a multicast listener is the same,
except that it does not throw the TooManyListenersException.
public void add<Event>Listener(<Event>Listener l)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deregistration Method
The naming pattern for removing a listener (either
unicast or multicast)
public void remove<Event>Listener(<Event>Listener l)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating Java Beans
Step 1: Create a Java Bean file e.g. “LightBulb.java”
Step 2: Compile the file: javac LightBulb.java
Step 3: Create a manifest file, named “manifest.tmp”
Step 4: Create the JAR file, named “LightBulb.jar”
Step 5: Load jar file.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Manifest File
Create text file manifest.tmp
 Manifest file describes contents of JAR file


Name: name of file with bean class
Java-Bean: true - file is a JavaBean
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating Jar file
c - creating JAR file
f - indicates next argument is name of file
m - next argument manifest.tmp file
Used to create MANIFEST.MF

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
.

.

More Related Content

What's hot

Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentalsmegharajk
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1İbrahim Kürce
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsBhushan Nagaraj
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 

What's hot (20)

Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentals
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Core java
Core javaCore java
Core java
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Java notes
Java notesJava notes
Java notes
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Cs8392 oops 5 units notes
Cs8392 oops 5 units notes Cs8392 oops 5 units notes
Cs8392 oops 5 units notes
 
Core Java
Core JavaCore Java
Core Java
 

Viewers also liked (20)

Javabeans
JavabeansJavabeans
Javabeans
 
Java beans
Java beansJava beans
Java beans
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Java Beans
Java BeansJava Beans
Java Beans
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
introduction of Java beans
introduction of Java beansintroduction of Java beans
introduction of Java beans
 
Event handling
Event handlingEvent handling
Event handling
 
javabeans
javabeansjavabeans
javabeans
 
Java beans
Java beansJava beans
Java beans
 
Java bean
Java beanJava bean
Java bean
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
Jsp java bean
Jsp   java beanJsp   java bean
Jsp java bean
 
Bài 5: Java Bean - Lập Trình Mạng Nâng Cao
Bài 5: Java Bean - Lập Trình Mạng Nâng CaoBài 5: Java Bean - Lập Trình Mạng Nâng Cao
Bài 5: Java Bean - Lập Trình Mạng Nâng Cao
 
Java beans
Java beansJava beans
Java beans
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Unit iv
Unit ivUnit iv
Unit iv
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Beans presentation
Beans presentationBeans presentation
Beans presentation
 
Java beans
Java beansJava beans
Java beans
 
Networking
NetworkingNetworking
Networking
 

Similar to Java beans

Similar to Java beans (20)

Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Inheritance
InheritanceInheritance
Inheritance
 
Generics
GenericsGenerics
Generics
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
The smartpath information systems java
The smartpath information systems javaThe smartpath information systems java
The smartpath information systems java
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
1_JavIntro
1_JavIntro1_JavIntro
1_JavIntro
 
Packages
PackagesPackages
Packages
 
EJB 3.0 Walkthrough (2006)
EJB 3.0 Walkthrough (2006)EJB 3.0 Walkthrough (2006)
EJB 3.0 Walkthrough (2006)
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practice
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Java beans

  • 1. Advanced Java Programming Topic: Java Beans By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Introduction  Every Java user interface class is a JavaBeans component.  Java Beans makes it easy to reuse software components.  Developers can use software components written by others without having to understand their inner workings. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Java Beans  JavaBeans is a software component architecture that extends the power of the Java language by enabling well-formed objects to be manipulated visually at design time in a pure Java builder tool.  It is a reusable software component.  A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Java Beans Specifications 1. 2. 3. 4. 5. A bean must be a public class. A bean must have a public no-arg constructor, though it can have other constructors if needed. public MyBean(); A bean must implement the Serializable interface to ensure a persistent state. It should provide methods to set and get the values of the properties, known as getter (accessor) and setter (mutator) methods. A bean may have events with correctly constructed public registration and deregistration methods that enable it to add and remove listeners. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Java Beans Specification  The first three requirements must be observed, and therefore are referred to as minimum JavaBeans component requirements.  The last two requirements depend on implementations.  It is possible to write a bean component without get/set methods and event registration/deregistration methods. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Java Beans Example public class StudentsBean implements java.io.Serializable { private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName() { return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setAge(Integer age){ this.age = age; } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Java Beans Component  The classes that define the beans, referred to as JavaBeans components or bean components.  A JavaBeans component is a serializable public class with a public no-arg constructor. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. Bean Properties  Properties are discrete, named attributes of a Java bean that can affect its appearance or behavior.  They are often data fields of a bean. For example, JButton component has a property named text.   Accessor and mutator methods are provided to let the user read and write the properties. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. Property-Naming Patterns  The bean property-naming pattern is a convention of the JavaBeans component model that simplifies the bean developer's task of presenting properties.  A property can be a primitive data type or an object type.  The property type dictates the signature of the accessor and mutator methods. Note: Properties describe the state of the bean. Naturally, data fields are used to store properties. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Accessor Methods  The accessor method is named get<PropertyName>(), which takes no parameters and returns a primitive type value or an object of a type identical to the property type.  public String getMessage() public int getXCoordinate() public int getYCoordinate()   Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11.  For a property of boolean type, the accessor method should be named is<PropertyName>( ) which returns a boolean value. For example: public boolean isCentered() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Mutator Methods  The mutator method should be named as set<PropertyName>(dataType p) which takes a single parameter identical to the property type and returns void.     public void setMessage(String s) public void setXCoordinate(int x) public void setYCoordinate(int y) public void setCentered(boolean centered) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Java EvEnt ModEl Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Event Delegation Model  The Java event delegation model provides the foundation for beans to send, receive, and handle events.  The Java event model consists of the following three types of elements: The event object: An event object contains the information that  describes the event.  The source object: A source object is where the event originates. When an event occurs on a source object, an event object is created.  The event listener object: An object interested in the event handles the event. Such an object is called a listener. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Event classes and Event Listener interfaces  An event object is created using an event class, such as ActionEvent, MouseEvent, and ItemEvent. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Source Components  The source component contains the code that detects an external or internal action that triggers the event.  Upon detecting the action, the source should fire an event to the listeners by invoking the event handler defined by the listeners.  The source component must also contain methods for registering and deregistering listeners. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. Listener Components  A listener component for an event must implement the event listener interface.  The object of the listener component cannot receive event notifications from a source component unless the object is registered as a listener of the source.  A listener component may implement any number of listener interfaces to listen to several types of events.  A source component may register many listeners. A source component may register itself as a listener. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Creating Custom Source Components  A source component must have the appropriate registration and deregistration methods for adding and removing listeners.  Events can be unicasted (only one listener object is notified of the event) or multicasted (each object in a list of listeners is notified of the event). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Unicast public void add<Event>Listener(<Event>Listener l) throws TooManyListenersException; Multicast The naming pattern for adding a multicast listener is the same, except that it does not throw the TooManyListenersException. public void add<Event>Listener(<Event>Listener l) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Deregistration Method The naming pattern for removing a listener (either unicast or multicast) public void remove<Event>Listener(<Event>Listener l) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Creating Java Beans Step 1: Create a Java Bean file e.g. “LightBulb.java” Step 2: Compile the file: javac LightBulb.java Step 3: Create a manifest file, named “manifest.tmp” Step 4: Create the JAR file, named “LightBulb.jar” Step 5: Load jar file. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Manifest File Create text file manifest.tmp  Manifest file describes contents of JAR file  Name: name of file with bean class Java-Bean: true - file is a JavaBean Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. Creating Jar file c - creating JAR file f - indicates next argument is name of file m - next argument manifest.tmp file Used to create MANIFEST.MF Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27. . .