You are on page 1of 43

JavaBeans

Topics

JavaBean as a component model Core concepts of JavaBeans Properties Event model Introspection Bean persistence Bean persistence in XML

JavaBean as a Software Component Model


3

Software Component

Software components are self-contained, reusable software units isual software components

!sin" visual application builder tools, visual software components can be composed into applets, applications, servlets, and composite components #ou perform t$is composition wit$in a "rap$ical user interface, and %ou can immediatel% see t$e results of %our wor&' Capture business lo"ic or state
4

(on-visual software components

What is a JavaBean?

JavaBeans) is a portable, platform-independent component model written in t$e Java pro"rammin" lan"ua"e *it$ t$e JavaBeans +PI %ou can create reusable, platform-independent components !sin" JavaBeans-compliant application builder tools suc$ as (etBeans or Eclipse, %ou can combine t$ese components into applets, applications, or composite components'

What is a JavaBean?

JavaBean components are &nown as beans' Beans are d%namic in t$at t$e% can be c$an"ed or customi,ed -$rou"$ t$e desi"n mode of a builder tool, %ou use t$e propert% s$eet or bean customi,er to customi,e t$e bean and t$en save .persist/ %our customi,ed beans'

Core Concepts of JavaBeans


7

Builder Tools & Introspection

Builder tools discover a bean0s features .t$at is, its properties, met$ods, and events/ b% a process &nown as introspection' Beans support introspection in two wa%s1

B% ad$erin" to specific rules, &nown as desi"n patterns, w$en namin" bean features B% e2plicitl% providin" propert%, met$od, and event information wit$ a related bean information class'

Properties

Properties are t$e appearance and be$avior c$aracteristics of a bean t$at can be c$an"ed at desi"n time Beans e2pose properties so t$e% can be customi,ed at desi"n time Builder tools introspect on a bean to discover its properties and e2pose t$ose properties for manipulation Customi,ation is supported in two wa%s1

b% usin" propert% editors b% usin" more sop$isticated bean customi,ers


9

Events

Beans use events to communicate wit$ ot$er beans + bean t$at is to receive events .a listener bean/ re"isters wit$ t$e bean t$at fires t$e event .a source bean/ Builder tools can e2amine a bean and determine w$ic$ events t$at bean can fire .send/ and w$ic$ it can $andle .receive/

10

Persistence

Persistence enables beans to save and restore t$eir state +fter c$an"in" a bean0s properties, %ou can save t$e state of t$e bean and restore t$at bean at a later time wit$ t$e propert% c$an"es intact -$e JavaBeans arc$itecture uses Java 3b4ect Seriali,ation to support persistence'

11

JavaBean Method

+ bean0s met$ods are no different from Java met$ods, and can be called from ot$er beans or a scriptin" environment B% default all public met$ods are e2ported

12

E amples of Beans

5!I ."rap$ical user interface/ component (on-visual beans, suc$ as a spellin" c$ec&er +nimation applet Spreads$eet application

13

E amples of !"I Beans

Button Beans

Slider Bean

14

Properties
15

Properties

+ bean propert% is a named attribute of a bean t$at can affect its be$avior or appearance E2amples of bean properties include color, label, font, font si,e, and displa% si,e'

16

T#pes of Properties

Simple 6 + bean propert% wit$ a sin"le value w$ose c$an"es are independent of c$an"es in an% ot$er propert%' Inde2ed 6 + bean propert% t$at supports a ran"e of values instead of a sin"le value' Bound 6 + bean propert% for w$ic$ a c$an"e to t$e propert% results in a notification bein" sent to some ot$er bean' Constrained 6 + bean propert% for w$ic$ a c$an"e to t$e propert% results in validation b% anot$er bean' -$e ot$er bean ma% re4ect t$e c$an"e if it is not appropriate' 17

Event Model
18

JavaBeans Event Model


Based t$e Java 7'7 event model +n ob4ect interested in receivin" events is an event listener 6 sometimes called event receiver +n ob4ect t$at "enerates .fire/ events is called an event source 6 sometimes called event sender Event listeners re"ister t$eir interest of receivin" events to t$e event source

Event source provides t$e met$ods for event listeners to call for re"istration

-$e event source maintains a list of listeners and invo&e t$em w$en an event occurs
19

$e%istration of Event &isteners

Event listeners are re"istered to t$e event source t$rou"$ t$e met$ods provided b% t$e event source

addXXXListener removeXXXListener

20

Steps of Writin% Event 'andlin%


7'*rite Event class

Create %our own custom event class, named XXXEvent or use an e2istin" event class -$ere are e2istin" event class .i'e' +ctionEvent/ *rite XXXListener interface and provide implementation class of it -$ere are built-in listerner interfaces .i'e' +ctionListener/ +dd an addXXXListener and removeXXXListener met$ods, w$ere XXX stands for t$e name of t$e event -$ese met$ods are used b% event listeners for re"istration -$ere are built-in event source classes 21

8'*rite Event listener .Event $andler or Event receiver/


9'*rite Event source .Event "enerator/


Steps of (ddin% Event 'andlin%


:'*rite a "lue class

;e"ister event listener to t$e event source t$rou"$ addXXXListener./ met$od of t$e event source

22

E ample )* Button 'andler

23

)+ Write Event Class

*e are "oin" to use ActionEvent class w$ic$ is alread% provided in J<=

24

,+ Write Event &istener Class

*e are "oin" to use ActionListener interface w$ic$ is alread% provided in J<= *e are "oin" to write ButtonHandler class w$ic$ implements ActionListener interface

25

,+ Write Event &istener Class


pu-lic class Button'andler implements (ction&istener . /00 0 Component that will contain messa%es a-out 0 events %enerated+ 0/ private JTe t(rea output1 /00 0 Creates an (ction&istener that will put messa%es in 0 JTe t(rea ever#time event received+ 0/ pu-lic Button'andler2 JTe t(rea output 3 . this+output 4 output1 5 /00 0 When receives action event notification6 appends 0 messa%e to the JTe t(rea passed into the constructor+ 0/ pu-lic void actionPerformed2 (ctionEvent event 3 . this+output+append2 7(ction occurred in the Button 'andler* 7 8 event 8 9:n9 3 1 26 5

;+ Write Event Source Class

*e are "oin" to use Button class w$ic$ is event source class and is alread% provided in J<= Button class alread% $as t$e followin" met$ods

add+ctionListener remove+ctionListener

27

<+ Write !lue Code


Create object instances Register event handler to the event source

28

<+ Write !lue Code


pu-lic class (ctionEventE ample . pu-lic static void main2Strin%=> ar%s3 . J?rame frame 4 new J?rame2 7Button 'andler7 31 JTe t(rea area 4 new JTe t(rea2 @6 AB 31 // Create event source o-Cect JButton -utton 4 new JButton2 7?ire Event7 31 // $e%ister an (ction&istener o-Cect to the event source -utton+add(ction&istener2 new Button'andler2 area 3 31

frame+add2 -utton6 Border&a#out+DE$T' 31 frame+add2 area6 Border&a#out+CEDTE$ 31 frame+pacF231 frame+setGefaultCloseEperation2 WindowConstants+GISPESEHEDHC&ES frame+set&ocation$elativeTo2 null 31 frame+setIisi-le2 true 31 5
29

What 'appens When an Event Eccurs?

Event source invo&es event $andlin" met$od of all Event $andlers .event listener/ re"istered to it

actionPerformed./ met$od Button>andler will be invo&ed

30

Introspection
31

What is Introspection?

Introspection is t$e automatic process of anal%,in" a bean0s desi"n patterns to reveal t$e bean0s properties, events, and met$ods

-$is process controls t$e publis$in" and discover% of bean operations and properties

B% default, introspection is supported b% reflection, w$ere %ou name met$ods wit$ certain namin" patterns, li&e set?"etPropert%./ and add?removeListener./

32

?eatureGescriptor

33

Thin%s That Can -e ?ound throu%h Introspection

Simple propert%

public void setPropert%(ame.Propert%-%pe value/@ public Propert%-%pe "etPropert%(ame./@ public void setPropert%(ame.boolean value/@ public boolean isPropert%(ame./@ public void setPropert%(ame.int inde2, Propert%-%pe value/@ public Propert%-%pe "etPropert%(ame.int inde2/@ public void setPropert%(ame.Propert%-%peAB value/@ public Propert%-%peAB "etPropert%(ame./@
34

Boolean propert%

Inde2ed propert%

Thin%s That can -e found throu%h Introspection

Multicast events

public void addEventListener-%pe.EventListener-%pe l/@ public void removeEventListener-%pe.EventListener-%pe l/@ public void addEventListener-%pe.EventListener-%pe l/ t$rows -ooMan%ListenersE2ception@ public void removeEventListener-%pe.EventListener-%pe l/@ public met$ods

!nicast events

Met$ods

35

Bean Persistence
36

Bean Persistence

-$rou"$ ob4ect seriali,ation 3b4ect seriali,ation means convertin" an ob4ect into a data stream and writin" it to stora"e' +n% applet, application, or tool t$at uses t$at bean can t$en CreconstituteC it b% deseriali,ation' -$e ob4ect is t$en restored to its ori"inal state Dor e2ample, a Java application can seriali,e a Drame window on a Microsoft *indows mac$ine, t$e seriali,ed file can be sent wit$ e-mail to a Solaris mac$ine, and t$en a Java application can restore t$e Drame window to t$e e2act state w$ic$ e2isted on t$e Microsoft *indows mac$ine' 37

Bean Persistence in XML


38

JM&Encoder Class

Enable beans to be saved in XML format -$e XMLEncoder class is assi"ned to write output files for te2tual representation of Seriali,able ob4ects
JM&Encoder encoder 4 new JM&Encoder2 new BufferedEutputStream2 new ?ileEutputStream2 7Beanarchive+ ml7 3 3 31 encoder+writeE-Cect2 o-Cect 31 encoder+close231
39

JM&Gecoder Class

XML<ecoder class reads an XML document t$at was created wit$ XMLEncoder1
JM&Gecoder decoder 4 new JM&Gecoder2 new BufferedInputStream2 new ?ileInputStream2 7Beanarchive+ ml7 3 3 31 E-Cect o-Cect 4 decoder+readE-Cect231 decoder+close231

40

E ample* SimpleBean
import 4ava'awt'Color@ import 4ava'beans'XML<ecoder@ import 4ava2'swin"'JLabel@ import 4ava'io'Seriali,able@ public class SimpleBean e2tends JLabel implements Seriali,able E public SimpleBean./ E set-e2t. C>ello worldFC /@ set3paGue. true /@ setBac&"round. Color';E< /@ setDore"round. Color'#ELL3* /@ set ertical+li"nment. CE(-E; /@ set>ori,ontal+li"nment. CE(-E; /@ H H

41

E ample* JM& $epresentation


IJ2ml versionKC7'LC encodin"KC!-D-MC JN I4avaN Iob4ect classKC4ava2'swin"'JDrameCN Ivoid met$odKCaddCN Iob4ect classKC4ava'awt'BorderLa%outC fieldKCCE(-E;C?N Iob4ect classKCSimpleBeanC?N I?voidN Ivoid propert%KCdefaultClose3perationCN Iob4ect classKC4ava2'swin"'*indowConstantsC fieldKC<ISP3SEO3(OCL3SEC?N I?voidN Ivoid met$odKCpac&C?N Ivoid propert%KCvisibleCN IbooleanNtrueI?booleanN I?voidN I?ob4ectN 42 I?4avaN

JavaBeans

43

You might also like