SlideShare a Scribd company logo
 An introduction to component-based
development in general
 Introduction to JavaBeans
 Java components
 client-side
 Working with the BDK
 The beans development life cycle
 Writing simple and advanced beans
 All engineering discplines use components to
build systems. In SE we rely on line-by-line SD.
 We have class libraries
 create objects from class libraries
 we still need to write a large amount of code
 objects are not enough
 They are like Integrated Circuit (IC)
components
 Over 20 years ago, hardware vendors learned
how to package transistors
 Hardware Engineers integrate ICs to make a
board of chips
 In SE, we are where hardware engineers were
20 years ago
 We are building software routines
 we can buy routines and use/reuse them in our
applications (assemble applications)
 JavaBeans -- portable, platform-independent
component model
 Java components are known as beans
 A bean: a reusable software component that
can be manipulated visually in a builder tool
 Beans are appropriate for software components
that can be visually manipulated
 Class libraries are good for providing
functionality that is useful to programmers,
and doesn’t benefit from visual manipulation
 A component is a self-contained reusable
software unit
 Components expose their features (public
methods and events) to builder tools
 A builder tool maintains Beans in a palette or
toolbox.
 You can select a bean from the toolbox, drop it
in a form, and modify its appearance and
behavior.
 Also, you can define its interaction with other
beans
 ALL this without a line of code.
 a public class with 0-argument constuctor
 it has properties with accessory methods
 it has events
 it can customized
 its state can be saved
 it can be analyzed by a builder tool
 A builder tool discover a bean’s features by a
process known as introspection.
 Adhering to specific rules (design pattern) when
naming Bean features.
 Providing property, method, and event information
with a related Bean Information class.
 Properties (bean’s appearance and behavior
characteristics) can be changed at design-time.
 Properties can be customized at design-time.
Customization can be done:
 using property editor
 using bean customizers
 Events are used when beans want to
intercommunicate
 Persistence: for saving and restoring the state
 Bean’s methods are regular Java methods.
 JavaBeans are subject to the standard Java
security model
 The security model has neither extended nor
relaxed.
 If a bean runs as an untrusted applet then it
will be subject to applet security
 If a bean runs as a stand-alone application then
it will be treated as a normal Java application.
 Assume your beans will be running in a multi-
threaded environment
 It is your responsibility (the developer) to make
sure that their beans behave properly under
multi-threaded access
 For simple beans, this can be handled by
simply making all methods …...
 To start the BeanBox:
 run.bat (Windows)
 run.sh (Unix)
 ToolBox contains the beans available
 BeanBox window is the form where you
visually wire beans together.
 Properties sheet: displays the properties for the
Bean currently selected within the BeanBox
window.
 Use screen captures from Windows
 Start application, following appears:
ToolBox has 16
sample
JavaBeans
BeanBox window
tests beans.
Properties
customizes
selected bean.
Method Tracer
displays debugging
messages (not
discussed)
 Initially, background selected
 Customize in Properties box
 Now, add JavaBean in BeanBox window
 Click ExplicitButton bean in ToolBox window
 Functions as a JButton
 Click crosshair where center of button should appear
 Change label to "Start the Animation"
 Select button (if not selected) and move to corner
 Position mouse on edges, move cursor appears
 Drag to new location
 Resize button
 Put mouse in corner, resize cursor
 Drag mouse to change size
 Add another button (same steps)
 "Stop the Animation"
 Add animation bean
 In ToolBox, select Juggler and add to BeanBox
 Animation begins immediately
Properties for
juggler.
 Now, "hook up" events from buttons
 Start and stop animation
 Edit menu
 Access to events from beans that are an event source
(bean can notify listener)
 Swing GUI components are beans
 Select "Stop the Animation"
 Edit->Events->button push -> actionPerformed
 Line appears from button to mouse
 Target selector - target of event
 Object with method we intend to call
 Connect the dots programming
 Click on Juggler, brings up EventTargetDialog
 Shows public methods
 Select stopJuggling
 Event hookup complete
 Writes new hookup/event adapter class
 Object of class registered as actionListener fro
button
 Can click button to stop animation
 Repeat for "Start the Animation" button
 Method startAnimation
 Save as design
 Can reloaded into BeanBox later
 Can have any file extension
 Opening
 Applet beans (like Juggler) begin executing
immediately
 import java.awt.*;
 import java.io.Serializable;
 public class FirstBean extends Canvas implements
Serializable {
 public FirstBean() {
 setSize(50,30);
 setBackground(Color.blue);
 }
 }
 Compile: javac FirstBean.java
 Create a manifest file:
 manifest.txt
 Name: FirstBean.class
 Java-Bean: True
 Create a jar file:
 jar cfm FirstBean.jar mani.txt FirstBean.class
import java.awt.*;
public class Spectrum extends Canvas {
private boolean vertical;
public Spectrum() {
vertical=true;
setSize (100,100);
}
public boolean getVertical() {
return (vertical);
}
public void setVertical(boolean vertical) {
this.vertical=vertical;
repaint ();
public void paint(Graphics g) {
float saturation=1.0f;
float brightness=1.0f;
Dimension d=getSize ();
if (vertical) {
for (int y=0;y<d.height;y++) {
float hue=(float) y/(d.height-1);
g.setColor(Color.getHSBColor(hue,saturation,brightness));
g.drawLine (0,y, d.width-1, y);
}
}
else {
for (int x=0;x<d.width; x++) {
float hue=(float) x/(d.height-1);
g.setColor(Color.getHSBColor(hue,
saturation,brightness));
g.drawLine (x, 0,x, d.width-1);
}
}
} // method ends
}// class ends
 The above Spectrum class is a subclass of
Canvas. The private boolean variable named
vertical is one of its properties. The constructor
initializes that property to true and sets the size
of the component.
Java beans
 Bean’s appearance and behavior -- changeable
at design time.
 They are private values
 Can be accessed through getter and setter
methods
 getter and setter methods must follow some
rules -- design patterns (documenting
experience)
Copyright © 2001 Qusay H. Mahmoud
 A builder tool can:
 discover a bean’s properties
 determine the properties’ read/write attribute
 locate an appropriate “property editor” for each type
 display the properties (in a sheet)
 alter the properties at design-time
Copyright © 2001 Qusay H. Mahmoud
 Simple
 Index: multiple-value properties
 Bound: provide event notification when value
changes
 Constrained: how proposed changes can be
okayed or vetoed by other object
Copyright © 2001 Qusay H. Mahmoud
 When a builder tool introspect your bean it
discovers two methods:
 public Color getColor()
 public void setColor(Color c)
 The builder tool knows that a property named
“Color” exists -- of type Color.
 It tries to locate a property editor for that type
to display the properties in a sheet.
Copyright © 2001 Qusay H. Mahmoud
 Adding a Color property
 Create and initialize a private instance variable
 private Color color = Color.blue;
 Write public getter & setter methods
 public Color getColor() {
 return color;
 }
 public void setColor(Color c) {
 color = c;
 repaint();
 }
Copyright © 2001 Qusay H. Mahmoud
 For a bean to be the source of an event, it must
implement methods that add and remove
listener objects for the type of the event:
 public void add<EventListenerType>(<EventListenerType> elt);
 same thing for remove
 These methods help a source Bean know where
to fire events.
Copyright © 2001 Qusay H. Mahmoud
 Source Bean fires events at the listeners using
method of those interfaces.
 Example: if a source Bean register
ActionListsener objects, it will fire events at
those objects by calling the actionPerformed
method on those listeners
Copyright © 2001 Qusay H. Mahmoud
 Implementing the BeanInfo interface allows
you to explicitly publish the events a Bean fires
Copyright © 2001 Qusay H. Mahmoud
 Question: how does a Bean exposes its features
in a property sheet?
 Answer: using java.beans.Introspector class
(which uses Core Reflection API)
 The discovery process is named
“introspection”
 OR you can associate a class that implements
the BeanInfo with your bean
Copyright © 2001 Qusay H. Mahmoud
 Why use BeanInfo then?
 Using BeanInfo you can:
 Expose features that you want to expose
Copyright © 2001 Qusay H. Mahmoud
 The appearance and behavior of a bean can be
customized at design time.
 Two ways to customize a bean:
 using a property editor
 each bean property has its own editor
 a bean’s property is displayed in a property sheet
 using customizers
 gives you complete GUI control over bean
customization
 used when property editors are not practical
Copyright © 2001 Qusay H. Mahmoud
 A property editor is a user interface for editing
a bean property. The property must have both,
read/write accessor methods.
 A property editor must implement the
PropertyEditor interface.
 PropertyEditorSupport does that already, so you
can extend it.
Copyright © 2001 Qusay H. Mahmoud
 If you provide a custom property editor class,
then you must refer to this class by calling
PropertyDescriptor.setPropertyEditorClass in a
BeanInfo class.
 Each bean may have a BeanInfo class which
customizes how the bean is to appear.
SimpleBeanInfo implements that interface
Copyright © 2001 Qusay H. Mahmoud
 JavaBeans are just the start of the Software
Components industry.
 This market is growing in both, quantity and
quality.
 To promote commercial quality java beans
components and tools, we should strive to
make our beans as reusable as possible.
 Here are a few guidelines...
Copyright © 2001 Qusay H. Mahmoud
 Creating beans
 Your bean class must provide a zero-argument
constructor. So, objects can be created using
Bean.instantiate();
 The bean must support persistence
 implement Serializable or Externalizable
Copyright © 2001 Qusay H. Mahmoud

More Related Content

What's hot (20)

PPTX
Introduction to OOP in Python
Aleksander Fabijan
 
PPT
Adapter pattern
Shakil Ahmed
 
PPTX
Java Beans
Ankit Desai
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Java beans
Rajkiran Mummadi
 
PPTX
Java servlets
yuvarani p
 
PPTX
Dependency injection - the right way
Thibaud Desodt
 
PPTX
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
PPT
DOT Net overview
chandrasekhardesireddi
 
PPT
Flyweight pattern
Shakil Ahmed
 
PPTX
Mvc vs mvp vs mvvm a guide on architecture presentation patterns
Concetto Labs
 
PPTX
Bootstrap PPT Part - 2
EPAM Systems
 
PPTX
Java script
Abhishek Kesharwani
 
PDF
Consumer driven Contract Test
Knoldus Inc.
 
PPT
Introduction to java beans
Hitesh Parmar
 
PPTX
Asp.net mvc filters
Eyal Vardi
 
PPTX
Std 12 Computer Chapter 3 Designing Simple Website using KompoZer
Nuzhat Memon
 
PDF
Sap bo-universe-design-beginner-s-guide-part-i
Amit Sharma
 
PPTX
Java Spring Framework
Mehul Jariwala
 
PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Introduction to OOP in Python
Aleksander Fabijan
 
Adapter pattern
Shakil Ahmed
 
Java Beans
Ankit Desai
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Java beans
Rajkiran Mummadi
 
Java servlets
yuvarani p
 
Dependency injection - the right way
Thibaud Desodt
 
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
DOT Net overview
chandrasekhardesireddi
 
Flyweight pattern
Shakil Ahmed
 
Mvc vs mvp vs mvvm a guide on architecture presentation patterns
Concetto Labs
 
Bootstrap PPT Part - 2
EPAM Systems
 
Java script
Abhishek Kesharwani
 
Consumer driven Contract Test
Knoldus Inc.
 
Introduction to java beans
Hitesh Parmar
 
Asp.net mvc filters
Eyal Vardi
 
Std 12 Computer Chapter 3 Designing Simple Website using KompoZer
Nuzhat Memon
 
Sap bo-universe-design-beginner-s-guide-part-i
Amit Sharma
 
Java Spring Framework
Mehul Jariwala
 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 

Viewers also liked (16)

PDF
javabeans
Arjun Shanka
 
PPT
Java beans
sptatslide
 
PPTX
Unit iv
bhushan_adavi
 
PPT
Java Networking
Ankit Desai
 
PPT
Beans presentation
manjusha ganesan
 
PPTX
Java beans
Bipin Bedi
 
PPTX
Santa Ana School.pptx
sofiathoma
 
PPT
Bean Intro
vikram singh
 
PPT
introduction of Java beans
shravan kumar upadhayay
 
PDF
Reflection and Introspection
adil raja
 
PDF
Java Serialization
imypraz
 
PDF
Java beans
Ravi Kant Sahu
 
PDF
Java Reflection Explained Simply
Ciaran McHale
 
PPT
Reflection in java
upen.rockin
 
PPT
.NET Vs J2EE
ravikirantummala2000
 
PDF
J2EE Introduction
Patroklos Papapetrou (Pat)
 
javabeans
Arjun Shanka
 
Java beans
sptatslide
 
Unit iv
bhushan_adavi
 
Java Networking
Ankit Desai
 
Beans presentation
manjusha ganesan
 
Java beans
Bipin Bedi
 
Santa Ana School.pptx
sofiathoma
 
Bean Intro
vikram singh
 
introduction of Java beans
shravan kumar upadhayay
 
Reflection and Introspection
adil raja
 
Java Serialization
imypraz
 
Java beans
Ravi Kant Sahu
 
Java Reflection Explained Simply
Ciaran McHale
 
Reflection in java
upen.rockin
 
.NET Vs J2EE
ravikirantummala2000
 
J2EE Introduction
Patroklos Papapetrou (Pat)
 
Ad

Similar to Java beans (20)

PDF
Javabeans .pdf
Rajkiran Mummadi
 
PPT
Java beans
Umair Liaqat
 
PDF
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
PPTX
Java Beans Unit 4(part 2)
Dr. SURBHI SAROHA
 
PDF
Test Bank for Java How to Program Late Objects 10th Edition Deitel 0132575655...
giloulaiz
 
PDF
Test Bank for Java How to Program Late Objects 10th Edition Deitel 0132575655...
qkqwaexc7911
 
PPTX
Code camp 2011 Getting Started with IOS, Una Daly
Una Daly
 
PDF
Java beans
Vaibhav Shukla
 
PPTX
iOS app dev Training - Session1
Hussain Behestee
 
PDF
Class 1 blog
Narcisa Velez
 
DOCX
UIAutomator
Sandip Ganguli
 
PPTX
SoapUI Pro Plugin Workshop #SoapUIPlugins
SmartBear
 
PPT
CommercialSystemsBahman.ppt
KalsoomTahir2
 
PPTX
Lecture 2 Styling and Layout in React Native.pptx
GevitaChinnaiah
 
PPT
Javabean1
Saransh Garg
 
PDF
Introducing PanelKit
Louis D'hauwe
 
PPTX
Unit4wt
vamsi krishna
 
PDF
UNIT-2-AJAVA.pdf
PriyanshiPrajapati27
 
PPTX
Model View Presenter (MVP) In Aspnet
rainynovember12
 
PDF
Introduction to Andriod Studio Lecture note: Android Development Lecture 1.pdf
AliyuIshaq2
 
Javabeans .pdf
Rajkiran Mummadi
 
Java beans
Umair Liaqat
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Java Beans Unit 4(part 2)
Dr. SURBHI SAROHA
 
Test Bank for Java How to Program Late Objects 10th Edition Deitel 0132575655...
giloulaiz
 
Test Bank for Java How to Program Late Objects 10th Edition Deitel 0132575655...
qkqwaexc7911
 
Code camp 2011 Getting Started with IOS, Una Daly
Una Daly
 
Java beans
Vaibhav Shukla
 
iOS app dev Training - Session1
Hussain Behestee
 
Class 1 blog
Narcisa Velez
 
UIAutomator
Sandip Ganguli
 
SoapUI Pro Plugin Workshop #SoapUIPlugins
SmartBear
 
CommercialSystemsBahman.ppt
KalsoomTahir2
 
Lecture 2 Styling and Layout in React Native.pptx
GevitaChinnaiah
 
Javabean1
Saransh Garg
 
Introducing PanelKit
Louis D'hauwe
 
Unit4wt
vamsi krishna
 
UNIT-2-AJAVA.pdf
PriyanshiPrajapati27
 
Model View Presenter (MVP) In Aspnet
rainynovember12
 
Introduction to Andriod Studio Lecture note: Android Development Lecture 1.pdf
AliyuIshaq2
 
Ad

More from Ramraj Choudhary (8)

PPTX
Pulse shaping
Ramraj Choudhary
 
PPTX
Mobile computing
Ramraj Choudhary
 
DOC
Cookies
Ramraj Choudhary
 
PPTX
Microprocessors
Ramraj Choudhary
 
PPTX
Joins
Ramraj Choudhary
 
PPTX
Joins
Ramraj Choudhary
 
PPTX
Grid computing
Ramraj Choudhary
 
Pulse shaping
Ramraj Choudhary
 
Mobile computing
Ramraj Choudhary
 
Microprocessors
Ramraj Choudhary
 
Grid computing
Ramraj Choudhary
 

Recently uploaded (20)

PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Java beans

  • 1.  An introduction to component-based development in general  Introduction to JavaBeans  Java components  client-side  Working with the BDK  The beans development life cycle  Writing simple and advanced beans
  • 2.  All engineering discplines use components to build systems. In SE we rely on line-by-line SD.  We have class libraries  create objects from class libraries  we still need to write a large amount of code  objects are not enough
  • 3.  They are like Integrated Circuit (IC) components  Over 20 years ago, hardware vendors learned how to package transistors  Hardware Engineers integrate ICs to make a board of chips  In SE, we are where hardware engineers were 20 years ago  We are building software routines
  • 4.  we can buy routines and use/reuse them in our applications (assemble applications)  JavaBeans -- portable, platform-independent component model  Java components are known as beans  A bean: a reusable software component that can be manipulated visually in a builder tool
  • 5.  Beans are appropriate for software components that can be visually manipulated  Class libraries are good for providing functionality that is useful to programmers, and doesn’t benefit from visual manipulation
  • 6.  A component is a self-contained reusable software unit  Components expose their features (public methods and events) to builder tools  A builder tool maintains Beans in a palette or toolbox.
  • 7.  You can select a bean from the toolbox, drop it in a form, and modify its appearance and behavior.  Also, you can define its interaction with other beans  ALL this without a line of code.
  • 8.  a public class with 0-argument constuctor  it has properties with accessory methods  it has events  it can customized  its state can be saved  it can be analyzed by a builder tool
  • 9.  A builder tool discover a bean’s features by a process known as introspection.  Adhering to specific rules (design pattern) when naming Bean features.  Providing property, method, and event information with a related Bean Information class.  Properties (bean’s appearance and behavior characteristics) can be changed at design-time.
  • 10.  Properties can be customized at design-time. Customization can be done:  using property editor  using bean customizers  Events are used when beans want to intercommunicate  Persistence: for saving and restoring the state  Bean’s methods are regular Java methods.
  • 11.  JavaBeans are subject to the standard Java security model  The security model has neither extended nor relaxed.  If a bean runs as an untrusted applet then it will be subject to applet security  If a bean runs as a stand-alone application then it will be treated as a normal Java application.
  • 12.  Assume your beans will be running in a multi- threaded environment  It is your responsibility (the developer) to make sure that their beans behave properly under multi-threaded access  For simple beans, this can be handled by simply making all methods …...
  • 13.  To start the BeanBox:  run.bat (Windows)  run.sh (Unix)
  • 14.  ToolBox contains the beans available  BeanBox window is the form where you visually wire beans together.  Properties sheet: displays the properties for the Bean currently selected within the BeanBox window.
  • 15.  Use screen captures from Windows  Start application, following appears: ToolBox has 16 sample JavaBeans BeanBox window tests beans. Properties customizes selected bean. Method Tracer displays debugging messages (not discussed)
  • 16.  Initially, background selected  Customize in Properties box
  • 17.  Now, add JavaBean in BeanBox window  Click ExplicitButton bean in ToolBox window  Functions as a JButton  Click crosshair where center of button should appear  Change label to "Start the Animation"
  • 18.  Select button (if not selected) and move to corner  Position mouse on edges, move cursor appears  Drag to new location  Resize button  Put mouse in corner, resize cursor  Drag mouse to change size
  • 19.  Add another button (same steps)  "Stop the Animation"  Add animation bean  In ToolBox, select Juggler and add to BeanBox  Animation begins immediately Properties for juggler.
  • 20.  Now, "hook up" events from buttons  Start and stop animation  Edit menu  Access to events from beans that are an event source (bean can notify listener)  Swing GUI components are beans  Select "Stop the Animation"  Edit->Events->button push -> actionPerformed
  • 21.  Line appears from button to mouse  Target selector - target of event  Object with method we intend to call  Connect the dots programming  Click on Juggler, brings up EventTargetDialog  Shows public methods  Select stopJuggling
  • 22.  Event hookup complete  Writes new hookup/event adapter class  Object of class registered as actionListener fro button  Can click button to stop animation  Repeat for "Start the Animation" button  Method startAnimation
  • 23.  Save as design  Can reloaded into BeanBox later  Can have any file extension  Opening  Applet beans (like Juggler) begin executing immediately
  • 24.  import java.awt.*;  import java.io.Serializable;  public class FirstBean extends Canvas implements Serializable {  public FirstBean() {  setSize(50,30);  setBackground(Color.blue);  }  }
  • 25.  Compile: javac FirstBean.java  Create a manifest file:  manifest.txt  Name: FirstBean.class  Java-Bean: True  Create a jar file:  jar cfm FirstBean.jar mani.txt FirstBean.class
  • 26. import java.awt.*; public class Spectrum extends Canvas { private boolean vertical; public Spectrum() { vertical=true; setSize (100,100); } public boolean getVertical() { return (vertical); } public void setVertical(boolean vertical) { this.vertical=vertical; repaint ();
  • 27. public void paint(Graphics g) { float saturation=1.0f; float brightness=1.0f; Dimension d=getSize (); if (vertical) { for (int y=0;y<d.height;y++) { float hue=(float) y/(d.height-1); g.setColor(Color.getHSBColor(hue,saturation,brightness)); g.drawLine (0,y, d.width-1, y); }
  • 28. } else { for (int x=0;x<d.width; x++) { float hue=(float) x/(d.height-1); g.setColor(Color.getHSBColor(hue, saturation,brightness)); g.drawLine (x, 0,x, d.width-1); } } } // method ends }// class ends
  • 29.  The above Spectrum class is a subclass of Canvas. The private boolean variable named vertical is one of its properties. The constructor initializes that property to true and sets the size of the component.
  • 31.  Bean’s appearance and behavior -- changeable at design time.  They are private values  Can be accessed through getter and setter methods  getter and setter methods must follow some rules -- design patterns (documenting experience) Copyright © 2001 Qusay H. Mahmoud
  • 32.  A builder tool can:  discover a bean’s properties  determine the properties’ read/write attribute  locate an appropriate “property editor” for each type  display the properties (in a sheet)  alter the properties at design-time Copyright © 2001 Qusay H. Mahmoud
  • 33.  Simple  Index: multiple-value properties  Bound: provide event notification when value changes  Constrained: how proposed changes can be okayed or vetoed by other object Copyright © 2001 Qusay H. Mahmoud
  • 34.  When a builder tool introspect your bean it discovers two methods:  public Color getColor()  public void setColor(Color c)  The builder tool knows that a property named “Color” exists -- of type Color.  It tries to locate a property editor for that type to display the properties in a sheet. Copyright © 2001 Qusay H. Mahmoud
  • 35.  Adding a Color property  Create and initialize a private instance variable  private Color color = Color.blue;  Write public getter & setter methods  public Color getColor() {  return color;  }  public void setColor(Color c) {  color = c;  repaint();  } Copyright © 2001 Qusay H. Mahmoud
  • 36.  For a bean to be the source of an event, it must implement methods that add and remove listener objects for the type of the event:  public void add<EventListenerType>(<EventListenerType> elt);  same thing for remove  These methods help a source Bean know where to fire events. Copyright © 2001 Qusay H. Mahmoud
  • 37.  Source Bean fires events at the listeners using method of those interfaces.  Example: if a source Bean register ActionListsener objects, it will fire events at those objects by calling the actionPerformed method on those listeners Copyright © 2001 Qusay H. Mahmoud
  • 38.  Implementing the BeanInfo interface allows you to explicitly publish the events a Bean fires Copyright © 2001 Qusay H. Mahmoud
  • 39.  Question: how does a Bean exposes its features in a property sheet?  Answer: using java.beans.Introspector class (which uses Core Reflection API)  The discovery process is named “introspection”  OR you can associate a class that implements the BeanInfo with your bean Copyright © 2001 Qusay H. Mahmoud
  • 40.  Why use BeanInfo then?  Using BeanInfo you can:  Expose features that you want to expose Copyright © 2001 Qusay H. Mahmoud
  • 41.  The appearance and behavior of a bean can be customized at design time.  Two ways to customize a bean:  using a property editor  each bean property has its own editor  a bean’s property is displayed in a property sheet  using customizers  gives you complete GUI control over bean customization  used when property editors are not practical Copyright © 2001 Qusay H. Mahmoud
  • 42.  A property editor is a user interface for editing a bean property. The property must have both, read/write accessor methods.  A property editor must implement the PropertyEditor interface.  PropertyEditorSupport does that already, so you can extend it. Copyright © 2001 Qusay H. Mahmoud
  • 43.  If you provide a custom property editor class, then you must refer to this class by calling PropertyDescriptor.setPropertyEditorClass in a BeanInfo class.  Each bean may have a BeanInfo class which customizes how the bean is to appear. SimpleBeanInfo implements that interface Copyright © 2001 Qusay H. Mahmoud
  • 44.  JavaBeans are just the start of the Software Components industry.  This market is growing in both, quantity and quality.  To promote commercial quality java beans components and tools, we should strive to make our beans as reusable as possible.  Here are a few guidelines... Copyright © 2001 Qusay H. Mahmoud
  • 45.  Creating beans  Your bean class must provide a zero-argument constructor. So, objects can be created using Bean.instantiate();  The bean must support persistence  implement Serializable or Externalizable Copyright © 2001 Qusay H. Mahmoud