You are on page 1of 284

Sun Educational Services

JavaBeans Component
Development

SL-292

JavaBeans Component Development January 1999


Course Contents

About This Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Preface-1


Course Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-2
Course Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-3
Course Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-4
Module-by-Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-5
Course Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-7
Skills Gained by Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-8
Guidelines for Module Pacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-9
Topics Not Covered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-10
How Prepared Are You? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-11
Introductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-12
How to Use Course Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-13
How to Use the Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-14
Typographical Conventions and Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preface-15

Overview of JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
What Is JavaBeans? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
What Is a Bean? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Design Goals of JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Component Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Putting Beans Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
The BDK 1.0 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
ActiveX as a Component Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13

JavaBeans Component Development ii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
What Is the BeanBox? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Windows of the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Design Mode in the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
Manipulating a Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Changing Bean Properties – PropertySheet Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Changing Bean Properties – Customizers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
Bound Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10
Connecting Beans With Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Saving and Restoring the BeanBox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
Adding Beans to the ToolBox Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Using JAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Sample Jamfile Makefile for JAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
Home Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18

Bean Event Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
What Is an Event? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Delegation Model Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
Simple Code Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5
Code Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Categories of Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Obtaining Details About the Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
Creating Your Own Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9
Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
Creating Your Own Listener Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Event Sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12

JavaBeans Component Development iii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Multicast Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13


Unicast Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Notifying All Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
Event Delivery Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
Recap of Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
Bean Components and Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
Stock Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19
StockPriceChangeEvent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20
StockWatcher Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21
StockDetail Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise: Working With the Bean Event Model 3-24
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26

Bean Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
What Is a Bean Property?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
Properties and get/set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Simple Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5
Adding Simple Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
PropertySheet and Edit Menu. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Bound Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Example of Creating a Bound Property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Bound Properties and the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Recap of Bound Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Constrained Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Example of Creating a Constrained Property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Constrained Properties and Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
Constrained Properties and the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
Recap of Constrained Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17

JavaBeans Component Development iv


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Properties and the BeanBox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18


Views on the PropertySheet Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19
Example of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-20
Exercise: Defining Bean Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23

Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1
Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Introduction to Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
Introspection Addresses Key Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Sample Uses for BeanInfo . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
The Introspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Summary of the Introspection Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
Naming Conventions for Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Naming Conventions for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
Naming Conventions for Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
How a Bean Is Analyzed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Beans.instantiate Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Instantiation Supports Customized Beans and Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
Introspector.getBeanInfo Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17
Information Discovered by getBeanInfo . . . . . . . . . . . . . . . . . . . 5-18
What Is BeanInfo? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20
SimpleBeanInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
A BeanInfo Class That Affects Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22
Using getAdditionalBeanInfo . . . . . . . . . . . . . . . . . . . . . . . 5-24
BeanInfo Class That Affects Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25
Details of How a BeanInfo Is Processed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-26
Available BeanInfo Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27

JavaBeans Component Development v


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Reflection and JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28


Additional Information on Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
Final Words on Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-30
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-31
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-32

Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1
Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Goals for Bean Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
Java Object Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
What Is and Is Not Saved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Input and Output Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6
Saving Beans to Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Retrieving Beans From Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
Creating a Java Beans Prototype. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9
Deserialization and Class Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
Recap of Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
Exercise: Creating a New Bean Through Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14

Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1
Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
What Can You Do Through Customization? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3
Property Sheets and Property Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4
Review of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
The PropertyEditor Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6
Overview of the PropertyEditor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7
Creating Your Own Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8
Null Argument Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9

JavaBeans Component Development vi


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Supporting setValue() . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10


Add and Remove PropertyChangeListeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
MultipleLine Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12
MultipleLine Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-13
MultipleLine Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14
Display Style Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15
Text Field Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Choice Component Display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-17
Sample Editor – BoolEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18
Extending PropertyEditorSupport . . . . . . . . . . . . . . . . . . . . . 7-19
Making Your Property Editor Known . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
BeanInfo for the MultilineLabel Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Recap of Property Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-23
Exercise: Creating a Property Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-24
Customizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-25
Implementing a Customizer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-26
Defining BeanInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-27
Extending Component/Implementing Customizer . . . . . . . . . . . . . . . 7-28
Adding and Removing PropertyChangeListeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-29
Defining setObject(). . . . . . . . . . . . . . . . . . . . . . . . . . . 7-30
Example of a Customizer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-31
Recap of Customizers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-34
Exercise: Creating a Customizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-35
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-36
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-37

Event Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
What Is an Event Adapter? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3
Adapters Used in the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4

JavaBeans Component Development vii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Types of Adapters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5


Adapter Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6
Differentiating Adapters From Normal Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7
Demultiplexing Adapter Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8
Appl.java Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9
Widgets.java Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10
ActionAdapter.java Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-11
Multiplexing Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
Overview of Multiplexer Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13
Exercise: Working With Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-15
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16

Distributed Computing With Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Motivating Scenarios for Distributed Bean Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3
JavaSoft Distributed Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
Definition of JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5
Overview of JDBC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6
BeanBox JDBC Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7
Advantages of DataBase-Related Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8
Definition of RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9
BeanBox RMI Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10
RMI Architecture Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11
RMI Exercise Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-13
Source Files Provided . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-14
Data.java File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15
DataFactory.java File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16
DataImpl.java File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
DataFactoryImpl.java . . . . . . . . . . . . . . . . . . . . . . . . . . 9-19

JavaBeans Component Development viii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Server Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20


CORBA/JavaIDL (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21
Exercise: Creating an RMI Client Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-24

Beans Outside the BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Options for Building Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3
Subclassing a Bean and Adding BeanInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-4
Composing a Bean From Other Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-5
Customizing and Saving a Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6
Restoring the Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7
Creating Applets and Applications With Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8
Issues for Applets That Are Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9
Delivering Your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10
Using JAR Files in HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-11
JAR Files With a Digital Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13
Digital Signatures and Certificates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
Fitting the Pieces Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-15
Security and Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-16
What Tools Are Involved?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17
For More Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18
Loading and Instantiating a Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19
The Class Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
The ClassLoader Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21
Instantiating a Bean in an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22
Instantiating a Bean in an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24
JarLoader Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-25
Hooking Beans Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-27

JavaBeans Component Development ix


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Writing Applets or Applications With Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-28


Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-30

Business Environment for JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
“Write once, run anywhere” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3
Component-Based Software Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4
Bridging JavaBeans to Other Component Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
InfoBus Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-6
Development Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-7
Visual Application Builder Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-8
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-9
Think Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10

Transitional Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-1


Module Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
What Are Transitional Beans?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Creating a Transitional Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-4
Example of Transitional Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-5
ButtonPushEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-6
Listener Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
OurButton Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8
JellyBean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-10
Flipper Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11
Things to Remember. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
Check Your Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-14

JavaBeans Component Development x


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

JavaBeans Component Development xi


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Preface

About This Course

JavaBeans Component Development January 1999


Sun Educational Services

Course Goal
Provide you with knowledge and skills to:
• Create reusable Bean components
• Create Bean properties
• Understand how introspection and reflection works
• Work with the Bean event model
• Customize and persist Beans

JavaBeans Component Development Preface, slide 2 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Course Overview
• Use Bean components to create new applications.
• Beans are created using conventions in the JavaBeans
API specification.
• Beans can bridge to non-Java component models.
• Beans run in any environment where Java technology is
supported.

JavaBeans Component Development Preface, slide 3 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Course Map
Introduction to JavaBeans

Overview of The Bean Event


The BeanBox
JavaBeans Model

Modifying and Persisting Beans

Bean Properties Introspection

Persistence Customization

Working With Beans

Event Adapters Distributed Computing


With Beans

Beans Outside of Business Environment


the BeanBox for JavaBeans

Optional

Transitional Beans

JavaBeans Component Development Preface, slide 4 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module-by-Module Overview
• Module 1 – Overview of JavaBeans
• Module 2 – The BeanBox
• Module 3 – The Bean Event Model
• Module 4 – Bean Properties
• Module 5 – Introspection
• Module 6 – Persistence
• Module 7 – Customization

JavaBeans Component Development Preface, slide 5 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module-by-Module Overview
• Module 8 – Event Adapters
• Module 9 – Distributed Computing With Beans
• Module 10 – Beans Outside of the BeanBox
• Module 11 – Business Environment for JavaBeans
• Module 12 – Transitional Beans (Optional)

JavaBeans Component Development Preface, slide 6 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Course Objectives
• Define a Bean component and describe why JavaBeans is a Java
component model.
• Package JavaBeans components into JAR files, add them to the
BeanBox tool palette, and test them in the BeanBox.
• Given a class that implements a specific listener interface, write the
appropriate event handling methods.
• Create a JavaBeans component with bound or constrained
properties.
• Describe how the introspection process works, including the
relevance to naming conventions and to menu options displayed
in the Beanbox.
• Write the required persistence mechanisms for a customized Bean
component.
• Control the configuration and customization of Bean components
through customizer classes, property editors, property sheets, and
BeanInfo classes.
• Create event adapters to modify event delivery between sources
and listeners.
• Develop Bean components as intelligent front-ends to network
servers using a network access mechanism (such as JDBC, RMI, or
CORBA).
• Create applets or applications using existing Bean components.
• Explain how JavaBeans components can be used with existing
component models such as ActiveX and OpenDoc.

JavaBeans Component Development Preface, slide 7 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Skills Gained by Module


Meaning of:
• Black boxes
• Gray boxes

Module
Skills Gained 1 2 3 4
Skill or Objective 1
Skill or Objective 2
Skill or Objective 3
Skill or Objective 4

JavaBeans Component Development Preface, slide 8 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Guidelines for Module Pacing

Module Day 1 Day 2 Day 3 Day 4

About This Course A.M.


Overview of JavaBeans A.M.
The BeanBox A.M
P.M.
The Bean Event Model P.M.
Bean Properties A.M.
Introspection P.M.
Persistence A.M.
Customization A.M
P.M.
Event Adapters P.M.
Distributed Computing With Beans A.M.
Beans Outside of the BeanBox P.M.
Business Environment for JavaBeans P.M.
Transitional Beans P.M.

JavaBeans Component Development Preface, slide 9 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Topics Not Covered


• Object-oriented concepts
• Object-oriented design and analysis
• Java language constructs
• Details on distributed programming APIs

JavaBeans Component Development Preface, slide 10 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

How Prepared Are You?


• Experienced Java programmer able to use AWT
components, layout managers, event handling?
• Able to implement interfaces and exception handling?
• Experienced with object-oriented programming
languages?
• Capable of designing an object-oriented model for a
problem?
• At ease with learning new APIs?
• Able to learn best from code example and technical
explanation?

JavaBeans Component Development Preface, slide 11 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Introductions
• Name
• Company affiliation
• Title, function, and job responsibility
• Distributed computing experience
• Component development experience
• Application builder tool experience
• Reasons for enrolling in this course
• Expectations for this course

JavaBeans Component Development Preface, slide 12 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

How to Use Course Materials


• Course Map
• Relevance
• Overhead Image
• Lecture
• Exercise
• Check Your Progress
• Think Beyond

JavaBeans Component Development Preface, slide 13 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

How to Use the Icons


• Demonstration

• Discussion

• Laboratory

• Reference

JavaBeans Component Development Preface, slide 14 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Typographical Conventions and


Symbols

Typeface or
Meaning Example
Symbol
AaBbCc123 The names of commands, Edit your .login file.
files, and directories; Use ls -a to list all files.
on-screen computer output system% You have mail.

AaBbCc123 What you type, contrasted system% su


with on-screen computer Password:
output
AaBbCc123 Command-line To delete a file, type rm
placeholder—replace with filename.
a real name or value

JavaBeans Component Development Preface, slide 15 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Typeface or
Meaning Example
Symbol
AaBbCc123 Book titles, new words or Read Chapter 6 in User’s
terms, or words to be Guide. These are called class
emphasized options.
You must be root to do this.

JavaBeans Component Development Preface, slide 16 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 1

Overview of JavaBeans

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 1, slide 2 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is JavaBeans?
• A Java component model
• JavaBeans APIs
• Extension of Java platform

JavaBeans Component Development Module 1, slide 3 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is a Bean?
• Definition
• Features of Beans
• Examples of Beans
• Classes and Beans

JavaBeans Component Development Module 1, slide 4 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Design Goals of JavaBeans


• Compact
• Leverage strengths of Java platform
• Easy to create and use
• Fully portable
• Flexible build-time component editors
• Leverage distributed computing mechanisms

JavaBeans Component Development Module 1, slide 5 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Component Architectures
• Why are they useful?
• Services of component models
• Component interface publishing and discovery
• Event handling
• Persistence
• Layout control
• Application builder support

JavaBeans Component Development Module 1, slide 6 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Putting Beans Together


Describe how the following Beans might be hooked together:
• Graphing or charting Bean
• Random number generator Bean
• Animation Bean
• Timer Bean

JavaBeans Component Development Module 1, slide 7 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The BDK 1.0 Overview


• The BDK provides a reference implementation of the
JavaBeans Specification and is intended for beans
developers and tool vendors.
• The BDK contains:
• A reference Bean container, the BeanBox
• Sample source code for developing JavaBeans
components
• An on line tutorial for developing JavaBeans

• The BDK is recommended for use with JDK 1.1.3 or


later.

JavaBeans Component Development Module 1, slide 8 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
The BDK 1.0 Overview

Hashed boundary
around the BeanBox
Bean indicates
the currently selected
Bean

JavaBeans Component Development Module 1, slide 9 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

ActiveX as a Component Model


• Overview

Component-based
application
ActiveX API
OLE OLE
control document

OCX Java
Components control Bean

• Advantages of ActiveX

JavaBeans Component Development Module 1, slide 10 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

JavaBeans and ActiveX Comparison


• Platform
• Heavyweight or lightweight
• Network device support
• Interoperability
• Software versioning and distribution
• Distributed computing
• Performance
• Security

JavaBeans Component Development Module 1, slide 11 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Define JavaBeans.
• What services must every component model provide?
• Why is JavaBeans called a Java component model?
• Describe the design goals for JavaBeans.
• What is a Bean?
• Compare and contrast JavaBeans and ActiveX as
component models.

JavaBeans Component Development Module 1, slide 12 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
Suppose you have written a few Java Beans.
You now wish to test how well your Beans work.
Can you do this within the BDK? Do you need a third-party
builder tool?

JavaBeans Component Development Module 1, slide 13 of 13


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 2

The BeanBox

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 2, slide 2 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is the BeanBox?


• Definition
• Running the BeanBox

JavaBeans Component Development Module 2, slide 3 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Windows of the BeanBox

Hashed boundary
around the BeanBox
Bean

JavaBeans Component Development Module 2, slide 4 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Windows of the BeanBox


• ToolBox window
• BeanBox window
• PropertySheet window

JavaBeans Component Development Module 2, slide 5 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Design Mode in the BeanBox


• BeanBox in design mode when started
• To disable: View ➤ Disable Design Mode
• What you can do in design mode
• What you can do in run-time mode

JavaBeans Component Development Module 2, slide 6 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Manipulating a Bean
• Placing a Bean on the BeanBox window
• Selecting a Bean
• Moving a Bean
• Resizing a Bean

JavaBeans Component Development Module 2, slide 7 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Changing Bean Properties –


PropertySheet Window
• Mechanics
• Effects on the BeanBox window

JavaBeans Component Development Module 2, slide 8 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Changing Bean Properties –


Customizers
• Customizer class
• Edit ➤ Customize option
• Example Beans with customizers

JavaBeans Component Development Module 2, slide 9 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Bound Properties
• Source Bean with the bound property
• Target Bean and target property
• Mechanics of connecting them
• Edit ➤ Bind property
• Selecting source property
• Selecting target Bean and target property
• What happens after Beans are connected

JavaBeans Component Development Module 2, slide 10 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Connecting Beans With Event


Handlers
1. Select source Bean
2. Select event using Edit ➤ Events submenu
3. Select target Bean
4. Select handler method

JavaBeans Component Development Module 2, slide 11 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Saving and Restoring the BeanBox


• The File ➤ Save option
• What is saved
• What type of file is created

JavaBeans Component Development Module 2, slide 12 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Adding Beans to the ToolBox Window


• Defining JAR files
• Specifying a JAR file in an HTML file
• Creating a JAR file

JavaBeans Component Development Module 2, slide 13 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Using JAM
• Description of JAM (Just Another Make)
• Jamfile and manifest.tmp templates

JavaBeans Component Development Module 2, slide 14 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Sample Jamfile Makefile for
JAM
1 # set the name of the JAR file
2 jar = twoSimple.jar;
3
4 # this sets the package name of the java files - this is used
5 # to check dependencies of all compiled classes
6 SetPackage sl292 twoSimpleBeans;
7
8 # list of java sources - if the files are in the package
9 # directory then set the global variable
10 # $(USE_FULL_PATH_FOR_SOURCE) to "TRUE"
11 classes = MyText.java
12 MyButton.java
13 LabelEvent.java
14 LabelListener.java ;
15 DEPENDS all : $(jar) ;
16
17 # add the package of the java files to the list of directories
18 # added to the JAR file
19 AddPackageToJar $(CURR_PACKAGE) ;
20
21 # set the manifest file name - manifest.tmp is normally used
22 SetManifest manifest.tmp ;
23
24 # build the JAR file
25 JarFile $(jar) : $(classes) ;
26
27 # copy the new jar file to the BDK jars directory
28 File $(BDK_JARS_DIR)$(SLASH)$(jar) : $(jar) ;

JavaBeans Component Development Module 2, slide 15 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Home Directory Structure


SL292files

work BDK bin


02-beanbox ... 10-outsideBB

and so on
JamfilesSource files course_examples solutions

get_started
jars demo beanbox

sunw
run command

and so on

JavaBeans Component Development Module 2, slide 16 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• What is the purpose of the BeanBox?
• Move and resize Beans on the Composition window of the BeanBox.
• Change Bean properties using the PropertySheet window of the BeanBox.
• Register a Bean as the listener of an event generated by another Bean in the
Composition window.
• Save and restore the current state of the BeanBox.
• What is a JAR file and how can it be used?
• Create a JAR file for a prewritten Bean and add it to the ToolBox window
of the BeanBox.

JavaBeans Component Development Module 2, slide 17 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
• How does the event handling work for Beans?
• How do you define your own events?
• How do you indicate you want to receive a particular
event?

JavaBeans Component Development Module 2, slide 18 of 18


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 3

Bean Event Model

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 3, slide 2 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is an Event?
• Definition
• Examples
• Window events
• Mouse events
• Keyboard events
• List events
• Scrolling events

JavaBeans Component Development Module 3, slide 3 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Delegation Model Overview


• Sources and listeners
• Propagating notification of events
Register listener

Event source Event Event listener


Fire
event

JavaBeans Component Development Module 3, slide 4 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Simple Code Example
1 public class ButtonHandler implements ActionListener {
2 /**
3 * Component that will contain messages about events generated.
4 */
5 TextArea output;
6
7 /**
8 * Creates an ActionListener that will put messages in TextArea
9 * everytime event received.
10 */
11 public ButtonHandler(TextArea c) { output = c; }
12
13 /**
14 * When receives action event notification, appends message to the
15 * TextArea passed into the constructor.
16 */
17 public void actionPerformed(ActionEvent e) {
18 output.appendText("Action occurred:" + e + "\n");
19 }
20 }
21
22 class ActionTester {
23 public static void main(String args[]) {
24 Frame f = new Frame("Button Handler");
25 TextArea area = new TextArea(6, 80);
26 Button button = new Button("Fire Event");
27
28 button.addActionListener(new ButtonHandler(area));
29 f.add(button, BorderLayout.NORTH);
30 f.add(area, BorderLayout.CENTER);
31 f.pack();
32 f.setVisible(true);
33 }
34 }

JavaBeans Component Development Module 3, slide 5 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Code Explanation
Key items in the code:

• Registering listeners for event using


addActionListener()

• Implementing the ActionListener interface

• Defining required ActionListener event handler,


actionPerformed()

JavaBeans Component Development Module 3, slide 6 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Categories of Events
• Category – XXX
• Action, Item, Mouse motion, Mouse button, Key,
Focus, Adjustment, Component, Window,
Container, Text
• Interface – XXXListener
• ActionListener, ItemListener, and so on
• Event – XXXEvent
• ActionEvent, ItemEvent, MouseEvent, and so on

JavaBeans Component Development Module 3, slide 7 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Obtaining Details About the Event


• All events have java.util.EventObject as a base
class
• Events have accessor methods
• Example: getSource() gets the object that generated
the event
• Check event classes in the java.awt.event package for
examples of events generated by Components in the
AWT

JavaBeans Component Development Module 3, slide 8 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating Your Own Event


• Extend java.util.EventObject or an AWT event
class
• Define any accessor methods for listeners to obtain
information about the event
• Example
1 import java.util.*;
2 public class StockPriceChangeEvent extends EventObject {
3 private Stock stock;
4
5 public StockPriceChangeEvent (Object source, Stock s) {
6 super(source);
7 stock = s;
8 }
9 public Stock getStock() {
10 return stock;
11 }
12 }

JavaBeans Component Development Module 3, slide 9 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Listeners
• Identify listeners
• Listener interfaces
• ActionListener Interface
1 package java.awt.event;
2 import java.util.EventListener;
3
4 /**
5 * The listener interface for receiving action events.
6 */
7 public interface ActionListener extends EventListener {
8 /**
9 * Invoked when an action occurs.
10 */
11 public void actionPerformed(ActionEvent e);
12 }

JavaBeans Component Development Module 3, slide 10 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating Your Own Listener Interface


• Extend java.util.EventListener
• Specify the handler method, with the event type as an
argument
• Example
1 import java.util.EventListener;
2
3 public interface StockPriceChangeListener extends EventListener {
4 public void priceChange(StockPriceChangeEvent e);
5 }

JavaBeans Component Development Module 3, slide 11 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Event Sources
• Common sources
• Creating your own source
• Identifying sources
• addXXXListener
• removeXXXListener

JavaBeans Component Development Module 3, slide 12 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Multicast Syntax
1 private Vector listeners = new Vector();
2
3 public void addStockPriceChangeListener(StockPriceChangeListener spcl) {
4 listeners.addElement(spcl);
5 }
6
7 public void
8 removeStockPriceChangeListener(StockPriceChangeListener spcl) {
9 listeners.removeElement(spcl);
10 }

JavaBeans Component Development Module 3, slide 13 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Unicast Syntax
1 private StockPriceChangeListener listener = null;
2
3 public void addStockPriceChangeListener(StockPriceChangeListener spcl)
4 throwsjava.util.TooManyListenersException
5 {
6 if (listener == null) {
7 listener = spcl;
8 } else {
9 throw new java.util.TooManyListenersException();
10 }
11 }
12
13 public void removeStockPriceChangeListener(StockPriceChangeListener spcl) {
14 if (listener == spcl) {
15 listener = null;
16 }
17 }

JavaBeans Component Development Module 3, slide 14 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Notifying All Listeners


1 protected void notifyStockEvent(Stock stock) {
2 // Generate your event
3 Vector lis;
4 StockPriceChangeEvent evt;
5 e = new StockPriceChangeEvent(this, stock);
6 lis = (Vector) listeners.clone();
7
8 // Fire the event to every listener
9 StockPriceChangeListener spcl;
10 for (int i = 0, len=l.size(); i < len; i++) {
11 spcl = (StockPriceChangeListener) lis.elementAt(i);
12 spcl.priceChange(evt);
13 }
14 }

JavaBeans Component Development Module 3, slide 15 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Event Delivery Issues


• Synchronous delivery
• Multiple listeners

JavaBeans Component Development Module 3, slide 16 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Recap of Event Model


• XXXListener interface
• XXXEvent
• Event source
• Event listener

JavaBeans Component Development Module 3, slide 17 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Bean Components and Event


Handling
• Beans connected in BeanBox using Edit ➤ Events
• Example stock market beans
• StockWatcher bean generates an event when the
price of stock changes
• StockDetail bean presents the information about a
stock; it can receive price change notifications

• StockWatcher bean is nonvisual

• The price of a stock can change every 5 seconds

JavaBeans Component Development Module 3, slide 18 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Stock Class
1 package sl292.stock;
2
3 public class Stock {
4 String company;
5 String symbol;
6 double price;
7
8 public Stock(String co, String sym, double p) {
9 company = co;
10 symbol = sym;
11 price = p;
12 }
13
14 public void setCompany(String co) { company = co; }
15
16 public String getCompany() { return company; }
17
18 public void setSymbol(String sym) { symbol = sym; }
19
20 public String getSymbol() { return symbol; }
21
22 public double getPrice() { return price; }
23
24 public void setPrice(double p) { price = p;}
25 }

JavaBeans Component Development Module 3, slide 19 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

StockPriceChangeEvent Code
1 package sl292.stock;
2
3 import java.util.EventObject;
4
5 public class StockPriceChangeEvent extends EventObject {
6 private Stock stock;
7
8 public StockPriceChangeEvent(Object source, Stock s) {
9 super(source);
10 stock = s;
11 }
12
13 public Stock getStock() { return stock; }
14 }

1 package sl292.stock;
2
3 import java.util.EventListener;
4
5 public interface StockPriceChangeListener extends EventListener {
6 public void priceChange(StockPriceChangeEvent e);
7 }

JavaBeans Component Development Module 3, slide 20 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
StockWatcher Code
1 package sl292.stock;
2
3 import java.awt.event.*;
4 import java.util.Vector;
5 import sl292.beans.*;
6
7 public class StockWatcher implements ActionListener {
8 Stock stock;
9 Vector listeners = new Vector();
10
11 public StockWatcher() {
12 this(new Stock("Sun Microsystems", "SUNW", 50));
13 }
14
15 public StockWatcher(Stock s) {
16 stock = s;
17 //create a timer, register, and make timer active.
18 TimerBean t = new TimerBean(5000);
19 t.addActionListener(this);
20 t.setActive(true);
21 }
22
23 public String getCompany() { return stock.getCompany(); }
24
25 public void setCompany(String co) { stock.setCompany(co); }
26
27 public String getSymbol() { return stock.getSymbol(); }
28
29 public void setSymbol(String s) { stock.setSymbol(s); }
30
31 public void setPrice(double p) { stock.setPrice(p); }
32
33 public double getPrice() { return stock.getPrice(); }

JavaBeans Component Development Module 3, slide 21 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
StockWatcher Code
34 public void actionPerformed(ActionEvent e) {
35 double random = Math.random();
36
37 if (random < .28) {
38 //price went down
39 stock.setPrice(stock.getPrice()-.25);
40 } else if (random > .7) {
41 //price went up
42 stock.setPrice(stock.getPrice()+.25);
43 } else {
44 return;
45 }
46 generateStockEvent();
47 }
48
49 public void
50 addStockPriceChangeListener(StockPriceChangeListener sl){
51 listeners.addElement(sl);
52 }
53
54 public void
55 removeStockPriceChangeListener(StockPriceChangeListener sl)
56 { listeners.removeElement(sl);
57 }
58
59 private void generateStockEvent() {
60 StockPriceChangeEvent event;
61 StockPriceChangeListener sl;
62 event = new StockPriceChangeEvent(this, stock);
63 Vector v = (Vector)listeners.clone();
64
65 for (int i=0, len=v.size(); i<len; i++) {
66 sl = (StockPriceChangeListener)v.elementAt(i);
67 sl.priceChange(event);
68 }
69 }
70 }

JavaBeans Component Development Module 3, slide 22 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
StockDetail Class
1 package sl292.stock;
2
3 import java.awt.TextArea;
4
5 public class StockDetail extends TextArea
6 implements StockPriceChangeListener
7 {
8 public StockDetail() {
9 this(null);
10 }
11
12 public StockDetail(Stock s) {
13 super(4, 30);
14 showDetail(s);
15 }
16
17 public void priceChange(StockPriceChangeEvent e) {
18 showDetail(e.getStock());
19 }
20
21 public void showDetail(Stock s) {
22 if (s == null) {
23 setText("");
24 return;
25 }
26
27 setText("Company: " + s.getCompany() + "\n");
28 appendText(" symbol: " + s.getSymbol() + "\n");
29 appendText(" price: " + s.getPrice());
30 }
31 }

JavaBeans Component Development Module 3, slide 23 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Working With the Bean Event


Model
• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 3, slide 24 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Define event, event source, and event listener.

• What is the difference between multicast and unicast


sources?

• Create a multicast or unicast source.

• Implement a specified listener interface.

• Create two simple Bean components in which one is a


listener for the events of the other.

JavaBeans Component Development Module 3, slide 25 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
In working with the BeanBox, different properties were
displayed in the PropertySheet window for each Bean you
selected on the Composition window.

• How do you create Bean properties?

• Are there different types of properties?

JavaBeans Component Development Module 3, slide 26 of 26


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 4

Bean Properties

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 4, slide 2 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is a Bean Property?


• Definition
• Types of Bean properties
• Simple
• Bound
• Constrained
• Indexed

JavaBeans Component Development Module 4, slide 3 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Properties and get/set Methods


• Read and write properties
• Read-only properties
• Read-write properties
• Write-only properties
• Naming conventions
• Property abc implies getAbc() and setAbc()
methods

JavaBeans Component Development Module 4, slide 4 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Simple Properties
• Defining simple properties
• Adding simple properties to a Bean

JavaBeans Component Development Module 4, slide 5 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Adding Simple Properties


25 //Simple properties
26 public Color getColor() {
27 return circleColor;
28 }
29
30 public void setColor(Color newColor) {
31 circleColor = newColor;
32 repaint();
33 }
34
35 public int getRadius() {
36 return radius;
37 }
38
39 public void setRadius(int r) {
40 radius = r;
41 circum = 2 * 3.14159 * radius;
42 System.out.println("Circumference: " + circum);
43 repaint();
44 }
45
46 // read-only property
47 public double getCircum() {
48 return circum;
49 }

JavaBeans Component Development Module 4, slide 6 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
PropertySheet and Edit
Menu

Color property

Radius property

JavaBeans Component Development Module 4, slide 7 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Bound Properties
• Defining bound properties
• PropertyChangeSupport class
• Modifying the property set method
• Fire the PropertyChangeEvent

JavaBeans Component Development Module 4, slide 8 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Example of Creating a Bound Property


15 private PropertyChangeSupport support = new
16 PropertyChangeSupport(this);
17
18 //registration methods for PropertyChangeListeners
19 public void addPropertyChangeLIstener(PropertyChangeListener pcl) {
20 support.addPropertyChangeListener(pc;
21 }
22
23 public void removePropertyChangeListener(PropertyChangeListener pcl) {
24 support.removePropertyChangeListener(pcl);
25 }

43 public void setColor(Color newColor) {


44 Color prevColor = circleColor;
45 circleColor = newColor;
46 support.firePropertyChange("color", prevColor, circleColor);
47 repaint();
48 }

JavaBeans Component Development Module 4, slide 9 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Bound Properties and the BeanBox


Edit menu change:

JavaBeans Component Development Module 4, slide 10 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Recap of Bound Properties

Code Pieces for Defining Bound Property abc

private ABCtype abc;


private PropertyChangeSupport support = new
PropertyChangeSupport(this);

//registration methods for PropertyChangeListeners


public void addPropertyChangeListener(PropertyChangeListener pl) {
support.addPropertyChangeListener(pl);
}

public void removePropertyChangeListener(PropertyChangeListener pl) {


support.removePropertyChangeListener(pl);
}

public ABCtype getABC(){


return abc;
}
public void setABC(ABCtype newABC){
ABCtype oldABC = abc;
abc = newABC;
support.firePropertyChange("abc", oldABC, newABC);
}

Listener Interface PropertyChangeListener


Event PropertyChangeEvent
Handler method propertyChange(PropertyChangeEvent evt)
Utility class PropertyChangeSupport

JavaBeans Component Development Module 4, slide 11 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Constrained Properties
• Overview
• Order things are done in setXXX
• Defining constrained properties
• set method throws PropertyVetoException
• Handling vetos
• What the source Bean is responsible for

JavaBeans Component Development Module 4, slide 12 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Constrained Properties
• Using the VetoableChangeSupport Class
• Utility class similar to PropertyChangeSupport
• What the utility class does for you
• Registering listeners
• Modifying the property set method
• Calls the fireVetoableChange() method of the
VetoableChangeSupport object

JavaBeans Component Development Module 4, slide 13 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Example of Creating a
Constrained Property
17 private VetoableChangeSupport vetos = new
18 VetoableChangeSupport(this);

28 // Registration methods for VetoableChangeListeners


29 public void addVetoableChangeListener(VetoableChangeListener vl) {
30 vetos.addVetoableChangeListener(vl);
31 }
32
33 public void removeVetoableChangeListener(VetoableChangeListener vl) {
34 vetos.removeVetoableChangeListener(vl);
35 }

45 public void setRadius(int r) throws PropertyVetoException {


46 int prevRadius = radius;
47 if (r < 0) {
48 System.out.println("Negative radius not allowed!";
49 } else {
50 vetos.fireVetoableChange("radius", new Integer(prevRadius),
51 new Integer(r));
52 // no one vetoed, so make the change
53 radius = r;
54 circum = 2 * 3.14159 * radius;
55 System.out.println("Circumference: " + circum);
56 repaint();
57 }
58 }

JavaBeans Component Development Module 4, slide 14 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Constrained Properties and Validation


• By source or listener?
• Validating the property change
• Listener implements VetoableChangeListener
• Listener defines vetoableChange
• vetoableChange validates proposed property value
• vetoableChange throws PropertyVetoException
if value unacceptable

JavaBeans Component Development Module 4, slide 15 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Constrained Properties and the


BeanBox
• VetoableChange added to Edit ➤ Events submenu
• No special Edit option as for bound properties

JavaBeans Component Development Module 4, slide 16 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Recap of Constrained
Properties
Code Pieces for Defining Constrained Property xyz

private XYZtype xyz;


private VetoableChangeSupport vetos = new
VetoableChangeSupport(this);

//registration methods for VetoableChangeListeners


public void addVetoableChangeListener(VetoableChangeListener vl) {
vetos.addVetoableChangeListener(vl);
}

public void removeVetoableChangeListener(VetoableChangeListener vl) {


vetos.removeVetoableChangeListener(vl);
}

public XYZtype getXYZ(){


return abc;
}
public void setXYZ(XYZtype newXYZ) throws PropertyVetoException{
XYZtype oldXYZ = xyz;
vetos.fireVetoableChange("xyz", oldXYZ, newXYZ);
xyz = newXYZ;
}

Listener Interface VetoableChangeListener


Event PropertyChangeEvent
Handler method vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException
Utility class VetoableChangeSupport

Listeners define vetoableChange(PropertyChangeEvent evt)


throws PropertyVetoException { ... }
The method will validate the proposed new value for property xyz and throw
PropertyVetoException if the value is not allowed.

JavaBeans Component Development Module 4, slide 17 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Properties and the BeanBox


• Discovering Bean properties
• Only read-write properties are displayed on the
PropertySheet window
• Properties on the PropertySheet window
• Property editors
• Editors provided with JavaBeans

JavaBeans Component Development Module 4, slide 18 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Views on the PropertySheet Window


• Views
• PropertyCanvas
• PropertySelector
• PropertyText
• How a view is determined

JavaBeans Component Development Module 4, slide 19 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Example of Views
OurButton Bean Properties

PropertyCanvas
views

PropertyText Dialog window


views

PropertySelector Molecule Bean Properties


views

JavaBeans Component Development Module 4, slide 20 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Defining Bean Properties


• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 4, slide 21 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• What is a Bean property and what are some examples of
Bean properties?

• Compare the different types of Bean properties.

• What are the naming conventions used for Bean


properties and why is this significant?

• What determines if a property is read-only, read-write,


or write-only?

• Create a Bean component with a bound property.

• Create a Bean component with a constrained property.

JavaBeans Component Development Module 4, slide 22 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
• What exactly is introspection?

• How does it work to discover the properties and


behaviors of a Bean?

JavaBeans Component Development Module 4, slide 23 of 23


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 5

Introspection

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 5, slide 2 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Introduction to Introspection
• What problem does introspection solve?
• Code integration problem
• JavaBeans vision
• Introspection makes vision a reality

JavaBeans Component Development Module 5, slide 3 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Introspection Addresses Key Issues


• Reuse affected by different coding styles used by
developers
• Event propagation model prior to JDK 1.1
• Lack of support for examination and invocation of
methods prior to JDK 1.1

JavaBeans Component Development Module 5, slide 4 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Definitions
• Reflection
• Naming conventions
• Introspection
• BeanInfo

JavaBeans Component Development Module 5, slide 5 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Sample Uses for BeanInfo


• Limit a long list
• Provide GIF images as an icon
• Add descriptive name for properties
• Affect advanced options
• Specify additional “smart” customizer classes

JavaBeans Component Development Module 5, slide 6 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The Introspector
• What is the Introspector?
• Follows a plan for filling out Descriptor classes
• Finds information using BeanInfo classes and
getBeanInfo()
• Uses Reflection API classes

JavaBeans Component Development Module 5, slide 7 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Summary of the Introspection Process


• Three key concepts
• Naming conventions
• BeanInfo
• Reflection
• Automatic analysis

JavaBeans Component Development Module 5, slide 8 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Naming Conventions for Properties


• Simple properties
• public PropertyType getPropertyName()
• public void setPropertyName(PropertyType a)
• Boolean properties
• public boolean isPropertyName()
• public void setPropertyName(boolean b)

JavaBeans Component Development Module 5, slide 9 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Naming Conventions for Properties


• Indexed properties
• public PropertyElement getPropertyName(int
index)
• public void setPropertyName(int index,
PropertyElement element)
• public PropertyElement[] getPropertyName()
• public void setPropertyName(PropertyElement[]
element)

JavaBeans Component Development Module 5, slide 10 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Naming Conventions for Events


• Multicast event sources
• public void
addEventNameListener(EventNameListener el)
• public void removeEventNameListener
(EventNameListener el)

JavaBeans Component Development Module 5, slide 11 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Naming Conventions for Events


• Unicast event sources
• public void
addEventNameListener(EventNameListener el)
throws TooManyListenersException
• public void removeEventNameListener
(EventNameListener el)

JavaBeans Component Development Module 5, slide 12 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Naming Conventions for Methods


• Accessibility and public methods
• Properties
• Events
• Capitalization rules

JavaBeans Component Development Module 5, slide 13 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

How a Bean Is Analyzed

JavaBeans Component Development Module 5, slide 14 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Beans.instantiate Method
• The builder tool instantiates a Bean using:
beanName = "beanco.chart.PieChart";
bean = (Component) Beans.instantiate( classLoader, beanName);

JavaBeans Component Development Module 5, slide 15 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Instantiation Supports Customized


Beans and Applets
• The second argument to Beans.instantiate can be:
• Serialized file
• Class file
• Applet

JavaBeans Component Development Module 5, slide 16 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Introspector.getBeanInfo Method
• Called after Beans.instantiate()
• Code similar to:
beanClass = Class.forName("beanco.chart.PieChart");
BeanInfo bi = Introspector.getBeanInfo(beanClass);

JavaBeans Component Development Module 5, slide 17 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Information Discovered by
getBeanInfo
CLASS: sl292.dataTable.DataTableBean

Properties:
rowsintgetRows/setRows
cellSizeintgetCellSize/setCellSize
foregroundclass java.awt.ColorgetForeground/setForeground
backgroundclass java.awt.ColorgetBackground/setBackground
fontclass java.awt.FontgetFont/setFont
nameclass java.lang.StringgetName/setName
colsintgetCols/setCols
constrainedbooleangetConstrained/setConstrained
...

Event sets:
vetoableChangeaddVetoableChangeListener/removeVetoableChangeListener
vetoableChange

mouseaddMouseListener/removeMouseListener
mouseClicked
mousePressed
mouseReleased
mouseEntered
mouseExited
...

JavaBeans Component Development Module 5, slide 18 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Information Discovered by
getBeanInfo
Methods:
public void sl292.dataTable.DataTableBean.
removeVetoableChangeListener(java.beans.VetoableChangeListener)
public synchronized void sl292.dataTable.DataTableBean.setRows
(int) throws java.beans.PropertyVetoException
public boolean sl292.dataTable.DataTableBean.getConstrained()
public void sl292.dataTable.DataTableBean.
addVetoableChangeListener(java.beans.VetoableChangeListener)
public int sl292.dataTable.DataTableBean.getCellSize()
public void sl292.dataTable.DataTableBean.
fireDataTableEvent(sl292.dataTable.DataTableEvent)
public synchronized void sl292.dataTable.DataTableBean.
setCellSize(int)
public synchronized void sl292.dataTable.DataTableBean.setCols(int)
throws java.beans.PropertyVetoException
public synchronized void sl292.dataTable.DataTableBean.
setConstrained(boolean)
public int sl292.dataTable.DataTableBean.getCols()
public int sl292.dataTable.DataTableBean.getRows()

JavaBeans Component Development Module 5, slide 19 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is BeanInfo?
• Designed to exist as a template
• A generic BeanInfo class is always built, even when
reflection is used

JavaBeans Component Development Module 5, slide 20 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

SimpleBeanInfo Class
• Implements the BeanInfo interface
• Defines methods specified by the interface to return
null:
• getBeanDescriptor()
• getAdditionalBeanInfo()
• getPropertyDescriptors()
• getDefaultPropertyIndex()
• getEventsSetDescriptors()
• getDefaultEventIndex()
• getMethodDescriptors()
• getIcon(int)
JavaBeans Component Development Module 5, slide 21 of 32
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

A BeanInfo Class That Affects


Properties
1 package sl292.circle;
2
3 import java.beans.*;
4
5 public class CircleBeanBeanInfo extends SimpleBeanInfo {
6 public PropertyDescriptor[] getPropertyDescriptors() {
7 try {
8 PropertyDescriptor props[] = {
9 new PropertyDescriptor("radius", CircleBean.class)};
10
11 props[0].setDisplayName("Radius of circle");
12 props[0].setBound(true);
13
14 return props;
15 } catch (IntrospectionException ex) {
16 ex.printStackTrace();
17 return super.getPropertyDescriptors();
18 }
19 }
20 }
21 }

JavaBeans Component Development Module 5, slide 22 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

A BeanInfo Class That Affects


Properties
• Overriding the getPropertyDescriptors method
• Limiting visible properties
• PropertyDescriptor class
• setDisplayName method
• setBound method

JavaBeans Component Development Module 5, slide 23 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Using getAdditionalBeanInfo
• How it can be used
1 package sl292.circle;
2 import java.beans.*;
3
4 public class CircleBeanBeanInfo extends SimpleBeanInfo {
5 public BeanInfo[] getAdditionalBeanInfo() {
6 return new BeanInfo[] { new CircleBeanAdditionalInfo() };
7 }
8 }
9 public class CircleBeanAdditionalInfo extends SimpleBeanInfo {
10 public PropertyDescriptor[] getPropertyDescriptors() {
11 try {
12 PropertyDescriptor props[] = {
13 new PropertyDescriptor("radius", CircleBean.class)};
14 props[0].setDisplayName("Radius of circle");
15 props[0].setBound(true);
16 return props;
17 } catch (IntrospectionException ex) {
18 ex.printStackTrace();
19 return super.getPropertyDescriptors();
20 }
21 }
22 }

JavaBeans Component Development Module 5, slide 24 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

BeanInfo Class That Affects Methods


• Limit number of methods for target Bean
• Stock example code:
1 package sl292.stock;
2
3 import java.beans.*;
4 import java.lang.reflect.Method;
5
6 public class StockDetailBeanInfo extends SimpleBeanInfo {
7 MethodDescriptor method(String name, Class arg)
8 throws NoSuchMethodException
9 {
10 Method m = StockDetail.class.getMethod(name, new Class[] {arg});
11 return new MethodDescriptor(m);
12 }
13 public MethodDescriptor[] getMethodDescriptors() {
14 try {
15 return new MethodDescriptor[] {
16 method("priceChange", StockPriceChangeEvent.class)};
17 } catch(NoSuchMethodException ex) {
18 ex.printStackTrace();
19 return super.getMethodDescriptors();
20 }
21 }
22 }

JavaBeans Component Development Module 5, slide 25 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Details of How a BeanInfo Is


Processed
• Probe BeanInfo classes for descriptor information
• Apply reflection and naming conventions

JavaBeans Component Development Module 5, slide 26 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Available BeanInfo Methods


In addition to the methods for the BeanInfo interface, refer to
the tables of methods for:
• BeanDescriptor class
• EventSetDescriptor class
• PropertyDescriptor class
• MethodDescriptor class
• FeatureDescriptor class

JavaBeans Component Development Module 5, slide 27 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Reflection and JavaBeans


• Advantages provided by reflection
• Major classes of the Reflection API

JavaBeans Component Development Module 5, slide 28 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Additional Information on
Introspection
• Public methods, reflections, and BeanInfo
• Security

JavaBeans Component Development Module 5, slide 29 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Final Words on Introspection


• Foundation concept for the JavaBeans component
model
• Portability
• Reuse

JavaBeans Component Development Module 5, slide 30 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Define introspection and reflection.

• Analyze the relationship between introspection and the


naming conventions used for properties, methods, and
events.

• List several ways in which supplying a class for a Bean


can improve its usability.

• Describe how the process of introspection works.

• Create BeanInfo classes for Bean components.

JavaBeans Component Development Module 5, slide 31 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
This module has given you a foundation to work from. Here
is a sketch of upcoming modules which will put to use many
of the methods and classes that were discussed in the course
of learning about introspection:

• Module 6 – Persistence, how Beans are reloaded from a


serialized state

• Module 7 – How to write a customizer and a property


editor

• Module 10 – How to build programs that use Beans


outside of the Beanbox

JavaBeans Component Development Module 5, slide 32 of 32


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 6

Persistence

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 6, slide 2 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Goals for Bean Storage


• General Java object storage
• Object Serialization API
• Definition of object serialization
• Bean storage
• Object serialization mechanism
• Externalization mechanism

JavaBeans Component Development Module 6, slide 3 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Java Object Serialization


• Serializable interface
• Classes that are serialized

JavaBeans Component Development Module 6, slide 4 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is and Is Not Saved


• Bean properties and internal state
• Pointers and event adapters
• Fields automatically serialized
• Fields not serialized
• The keyword transient

JavaBeans Component Development Module 6, slide 5 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Input and Output
Interfaces
• ObjectOutput and ObjectInput
interfaces
java.io.DataOutput

ObjectOutput ObjectOutputStream

java.io.DataInput

ObjectInput ObjectInputStream

Legend
Class
Interface
Extends
Implements

JavaBeans Component Development Module 6, slide 6 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Saving Beans to Streams


• ObjectOutputStream class
• writeObject() method
• Example of code syntax:
1 public static void main(String args[]) {
2 String serFile = "day.ser";
3
4 try {
5 Date today = new Date();
6
7 FileOutputStream fos = new FileOutputStream(serFile);
8 ObjectOutputStream oos = new ObjectOutputStream(fos);
9 oos.writeObject(today);
10
11 oos.close();
12 fos.close();
13 } catch(IOException e) {
14 e.printStackTrace();
15 }
16 }

JavaBeans Component Development Module 6, slide 7 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Retrieving Beans From Streams


• ObjectInputStream class provides for deserializing
persisted objects
• readObject() method deserializes objects
1 public static void main(String args[]) {
2 try {
3 FileInputStream fis = new FileInputStream("day.ser");
4 ObjectInputStream ois = new ObjectInputStream(fis);
5
6 Date yesterday = (Date)ois.readObject();
7 System.out.println("The date was: "+ yesterday);
8
9 ois.close();
10 fos.close();
11 } catch(IOException e) {
12 e.printStackTrace();
13 } catch(ClassNotFoundException ex) {
14 //thrown if cannot locate a class used by serialized object
15 ex.printStackTrace();
16 }
17 }

JavaBeans Component Development Module 6, slide 8 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating a Java Beans Prototype


• Prototypes of Java Beans may be created using
serialization
• A prototype is created by:
• Serializing a bean to a file
• Providing a manifest file
• Building a JAR file containing the serialized bean and
the manifest file

• Allows an existing bean to be customized and its state


reused without writing any code

JavaBeans Component Development Module 6, slide 9 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Deserialization and Class Loaders


• Beans can be instantiated directly by using the
Beans.instantiate() method
• Method searches for a serialized object file, then a class
file
• Example of sl292.GreenBean would cause a search
for:
• SL292/GreenBean.ser
• SL292/GreenBean.class
1 public static void main(String args[]) throws Exception {
2 Component c = (Component)Beans.instantiate(null, "sl292.GreenBean");
3 Frame f = new Frame();
4 f.add(c, BorderLayout.CENTER);
5 f.setSize(200, 200);
6 f.show();
7 }

JavaBeans Component Development Module 6, slide 10 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Recap of Persistence
Writing to a Serialized Stream

String serFile = "filename.ser";


try {
FileOutputStream fileOStream = new FileOutputStream
serFile);
ObjectOutputStream os = new ObjectOutputStream
fileOStream);
os.writeObject(yourBeanObject);
os.close();
} catch (Exception e) {
e.printStackTrace();
}

Reading From a Serialized Stream


String serFile = "filename.ser";
try {
FileInputStream fileIStream = new FileInputStream
serFile);
ObjectInputStream is = new ObjectInputStream
fileIStream);
BeanType yourBeanRestored = (BeanType) is.readObject();
is.close();
} catch (Exception e) {
e.printStackTrace();
}

JavaBeans Component Development Module 6, slide 11 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Creating a New Bean


Through Serialization
• Objective
• Tasks
• Exercise summary

JavaBeans Component Development Module 6, slide 12 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• What are the goals for Bean storage?

• Describe how automatic Java serialization works.

• Using the serializable rules, what data fields should be


marked as transient and why?

• Describe the requirements to create persistent storage of


objects.

• Add the mechanisms to persist a Bean component.

JavaBeans Component Development Module 6, slide 13 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
The BeanBox provides a PropertySheet window for use in
modifying the properties of a Bean and a set of property
editors are also provided.

• Can you define your own property editor or property


sheet?

• Is there some other way to customize a Bean?

JavaBeans Component Development Module 6, slide 14 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 7

Customization

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 7, slide 2 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Can You Do Through


Customization?
• Definition of customization
• How do previous module relate to customization?
• Property sheets
• Can you extend editor support to new property types?
• Property editors

• When is a property-specific editor not enough?

• Customizers

JavaBeans Component Development Module 7, slide 3 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Property Sheets and Property Editors


• Property sheets
• Definition
• How property sheets work—builder tools
• Property editors
• Associated with properties for editing
• Editors provided by the API

JavaBeans Component Development Module 7, slide 4 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Review of Views

OurButton Bean Properties

PropertyCanvas
views

PropertyText
views Dialog window

PropertySelector Molecule Bean Properties


views

JavaBeans Component Development Module 7, slide 5 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The PropertyEditor Interface


• Implemented by any class that wants to provide editing
support for a given property type
• Implementations must support:
• One or more of the three simple display styles
• A call to setValue() when parameter is of the
proper type
• A custom editor or support setAsText()
• A null constructor

JavaBeans Component Development Module 7, slide 6 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Overview of the PropertyEditor


Methods
• get/setAsText()
• getCustomEditor()
• getJavaInitializationString()
• getTags()
• get/setValue()
• isPaintable()
• paintValue()
• supportsCustomEditor()

JavaBeans Component Development Module 7, slide 7 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating Your Own Editor


• Two ways to create your own editor
• Implement PropertyEditor interface
• Extend PropertyEditorSupport
• Implementing the PropertyEditor interface

JavaBeans Component Development Module 7, slide 8 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Null Argument Constructor


1 import java.beans.*;
2
3 public class LabelEditor implements PropertyEditor {
4 /**
5 * Property editors must provide a default constructor. Strictly
6 * speaking, this constructor could have been generated by
7 * the compiler.
8 public LabelEditor() { }
9
10 . . .
11 }

JavaBeans Component Development Module 7, slide 9 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Supporting setValue()
1 public class LabelEditor implements PropertyEditor {
2 String value;
3
4 public void setValue(Object value) {
5 value = (String)value;
6 }

JavaBeans Component Development Module 7, slide 10 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Add and Remove


PropertyChangeListeners
// In the most common case, the support can be set globally this way.
private PropertyChangeSupport support = new PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener pl) {


support.addPropertyChangeListener(pl);
}

public void removePropertyChangeListener(PropertyChangeListener pl) {


support.removePropertyChangeListener(pl);
}

JavaBeans Component Development Module 7, slide 11 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
MultipleLine Label
1 package sl292.beans;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.beans.*;
6
7 public class LabelEditor implements PropertyEditor {
8 String value;
9
10 // Property editors must provide a default constructor.
11 public LabelEditor() { }
12
13 // Set (or change) the object that is to be edited. Built-in types
14 // such as "int" must be wrapped as the corresponding object type
15 // such as "java.lang.Integer".
16 public void setValue(Object value) { value = (String)value; }
17
18 // Return the value of the property.
19 public Object getValue() { return value; }
20
21 // Return true if the class will honor the paintValue method.
22 public boolean isPaintable() { return true; }
23
24 // Paint a representation of the value into a given area of screen
25 // real estate. Note that the propertyEditor is responsible for
26 // doing its own clipping so that it fits into the given rectangle.
27 public void paintValue(Graphics gfx, Rectangle box) {
28 gfx.setClip(box);
29 FontMetrics fm = gfx.getFontMetrics();
30 gfx.drawString("Click to edit...",
31 box.x + 5,
32 (box.y + box.height + fm.getAscent())/2);
33 }
34

JavaBeans Component Development Module 7, slide 12 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
MultipleLine Label
35
36 // This method is intended for use when generating Java code to
37 // set the value of the property.
38 public String getJavaInitializationString() { return value; }
39
40 // Return the property value as a human editable string.
41 public String getAsText() { return value; }
42
43 // Set the property value by parsing a given String.
44 public void setAsText(String text) {
45 value = text;
46 }
47
48 // If the property value must be one of a set of known tagged
49 // values,then this method should return an array of the tags.
50 public String[] getTags() { return null; }
51
52 // A PropertyEditor may choose to make available a full custom
53 // Component that edits its property value.
54 public Component getCustomEditor() {
55 final TextArea labelArea = new TextArea(value);
56 labelArea.setSize(300, 150);
57 LabelArea.addTextListener(new TextListener()
58 {
59 public void textValueChanged(TextEvent e) {
60 value = labelArea.getText();
61 listeners.firePropertyChange(null, null, null);
62 }
63 } );
64 return labelArea;
65 }
66
67 // Return true if the propertyEditor can provide a custom editor.
68 public boolean supportsCustomEditor() { return true; }
69

JavaBeans Component Development Module 7, slide 13 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
MultipleLine Label
70
71 PropertyChangeSupport listeners = new
PropertyChangeSupport(this);
72
73 // Register a listener for the PropertyChange event.
74 public void addPropertyChangeListener(PropertyChangeListener pl){
75 listeners.addPropertyChangeListener(pl);
76 }
77
78 // Remove a listener for the PropertyChange event.
79 public void removePropertyChangeListener
(PropertyChangeListener pl){
80 listeners.removePropertyChangeListener(pl);
81 }
82 }
83

JavaBeans Component Development Module 7, slide 14 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Display Style
Requirements
• Graphic region display
• paintValue()
• isPaintable()
• supportsCustomEditor()
public boolean supportsCustomEditor() { return true; }

public Component getCustomEditor() {


final TextArea labelArea = new TextArea(value);
labelArea.setSize(300, 150);
LabelArea.addTextListener(new TextListener()
{
public void textValueChanged(TextEvent e) {
value = labelArea.getText();
listeners.firePropertyChange(null, null, null);
}
});
return labelArea;
}
public void paintValue(Graphics gfx, Rectangle box) {
gfx.setClip(box);
FontMetrics fm = gfx.getFontMetrics();
gfx.drawString("Click to edit...",
box.x + 5,
(box.y + box.height + fm.getAscent()) /2 );
}

JavaBeans Component Development Module 7, slide 15 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Text Field Display


• get/setAsText()
1 package sun.beans.editors;
2
3 import java.beans.*;
4
5 public class StringEditor extends PropertyEditorSupport {
6
7 public String getJavaInitializationString() {
8 return "\"" + getValue() + "\"";
9 }
10
11 public void setAsText(String text) {
12 setValue(text);
13 }
14 }

JavaBeans Component Development Module 7, slide 16 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Choice Component Display


• Return a non-null from:

• getTags()
• getAsText()

JavaBeans Component Development Module 7, slide 17 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Sample Editor – BoolEditor
1 package sun.beans.editors;
2
3 // Property editor for a built-in java "boolean" type.
4
5 import java.beans.*;
6 public class BoolEditor extends PropertyEditorSupport {
7
8 public String getJavaInitializationString() {
9 // This must return local independent Java.
10 if (((Boolean)getValue()).booleanValue()) {
11 return ("true");
12 } else {
13 return ("false");
14 }
15 }
16 public String getAsText() {
17
18 if (((Boolean)getValue()).booleanValue()) {
19 return ("True");
20 } else {
21 return ("False");
22 }
23 }
24 public void setAsText(String text) {
25 if (text.toLowerCase().equals("true")) {
26 setValue(Boolean.TRUE);
27 } else if (text.toLowerCase().equals("false")) {
28 setValue(Boolean.FALSE);
29 } else {
30 throw new java.lang.IllegalArgumentException(text);
31 }
32 }
33 public String[] getTags() {
34 String result[] = { "True", "False" };
35 return result;
36 }
37 }

JavaBeans Component Development Module 7, slide 18 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Extending PropertyEditorSupport
• A utility class that implements PropertyEditor
interface
• When to use
• Provided editors that extend PropertyEditorSupport
• BoolEditor
• StringEditor
• NumberEditor

JavaBeans Component Development Module 7, slide 19 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Making Your Property Editor Known


• PropertyEditorManager searches
• Naming conventions
• BeanInfo file

JavaBeans Component Development Module 7, slide 20 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
BeanInfo for the
MultilineLabel Bean
1 package sl292.beans;
2
3 import java.beans.*;
4 import java.awt.*;
5
6 public class MultilineLabelBeanInfo extends SimpleBeanInfo {
7 public BeanDescriptor getBeanDescriptor() {
8 return new BeanDescriptor(MultilineLabel.class);
9 }
10
11 PropertyDescriptor property(String name, String desc)
12 throws IntrospectionException
13 {
14 PropertyDescriptor p = new PropertyDescriptor(name,
15 MultilineLabel.class);
16 p.setShortDescription(desc);
17 return p;
18 }
19
20 public PropertyDescriptor[] getPropertyDescriptors() {
21 try {
22 PropertyDescriptor props[] = {
23 property("label", "The contents of the control"),
24 property("eolStyle",
25 "The method used to determine end of lines"),
26 property("alignment", "Where to align text"),
27 property("preferredWidth",
28 "The number of characters for width"),
29 property("font", "Font of label"),
30 property("background", "Background color of label"),
31 property("foreground", "Foreground color of label"),
32 property("name", "Name of component")
33 };
34
35 props[0].setPropertyEditorClass(LabelEditor.class);
36 props[1].setPropertyEditorClass(EolStyleEditor.class);
37 props[2].setPropertyEditorClass(AlignmentStyleEditor.class);

JavaBeans Component Development Module 7, slide 21 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
BeanInfo for the
MultilineLabel Bean
38 return props;
39 } catch(IntrospectionException ex) {
40 ex.printStackTrace();
41 return super.getPropertyDescriptors();
42 }
43
44 public int getDefaultPropertyIndex() { return 0; }
45 }
46 }

JavaBeans Component Development Module 7, slide 22 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Recap of Property Editors

Creating Your Own Property Editor

Extend PropertyEditorSupport Class

Decide what display style your editor will use

Define the appropriate methods for that display style: isPaintable(),


supportsCustomEditor(), getAsTest(), setAsText(), or getTags().

Define a BeanInfo file to register your property editor, if necessary.

Implement PropertyEditor Interface

Provide a null argument constructor

Define the setValue(Object o) method.

Support the addition and removal of PropertyChangeListeners:

private PropertyChangeSupport support = new


PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener pl)


{ support.addPropertyChangeListener(l);
}

public void removePropertyChangeListener


(PropertyChangeListener pl) {
support.removePropertyChangeListener(l);
}

Define the methods specified by the interface (some may be no-ops).

JavaBeans Component Development Module 7, slide 23 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Exercise: Creating a
Property Editor
• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 7, slide 24 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Customizers
• Customizer use
• Characteristics of customizers
• Naming conventions

JavaBeans Component Development Module 7, slide 25 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Implementing a Customizer Class


• Defining BeanInfo
• Extending Component
• Implementing Customizer
• Providing a null argument constructor
• Adding and removing PropertyChangeListeners
• Defining setObject()

JavaBeans Component Development Module 7, slide 26 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Defining BeanInfo
import java.beans.*;

public class MyBeanBeanInfo extends SimpleBeanInfo() {


public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(MyBean.class, MyBeanCustomizer.class);
}
}

JavaBeans Component Development Module 7, slide 27 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Extending Component/Implementing
Customizer
• Extend Component or its subclasses
• Implement the Customizer interface
• Use a null argument constructor
import java.beans.*;

public class MyBeanCustomizer extends Panel implements Customizer {


public MyBeanCustomizer() {
// Code to build and add GUI of the customizer
}
// All the other methods and code...
}

JavaBeans Component Development Module 7, slide 28 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Adding and Removing


PropertyChangeListeners
// In the most common case the support can be set globally this way.
private PropertyChangeSupport support = new PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener pl) {


support.addPropertyChangeListener(pl);
}

public void removePropertyChangeListener(PropertyChangeListener pl) {


support.removePropertyChangeListener(pl);
}

JavaBeans Component Development Module 7, slide 29 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Defining setObject()
public void setObject(Object beanToCustomize) {
private MyBean bean = (MyBean) beanToCustomize;
// Code to get current properties for "bean" and/or
// Code to build GUI elements for customizer and add them to layout.
}

JavaBeans Component Development Module 7, slide 30 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Example of a Customizer
1 package sunw.demo.buttons;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.beans.*;
6
7 public class OurButtonCustomizer extends Panel
8 implements Customizer, KeyListener
9 {
10 public OurButtonCustomizer() {
11 setLayout(null);
12 }
13
14 public void setObject(Object obj) {
15 target = (OurButton) obj;
16
17 Label t1 = new Label("Caption:", Label.RIGHT);
18 add(t1);
19 t1.setBounds(10, 5, 60, 30);
20
21 labelField = new TextField(target.getLabel(), 20);
22 add(labelField);
23 labelField.setBounds(80, 5, 100, 30);
24
25 labelField.addKeyListener(this);
26 }

JavaBeans Component Development Module 7, slide 31 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
27
28 public Dimension getPreferredSize() {
29 return new Dimension(200, 40);
30 }
31
32 /**
33 * @deprecated provided for backward compatibility with old layout
34 * managers.
35 */
36 public Dimension preferredSize() {
37 return getPreferredSize();
38 }
39

JavaBeans Component Development Module 7, slide 32 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Example of a Customizer
40 public void keyTyped(KeyEvent e) {
41 String txt = labelField.getText();
42 target.setLabel(txt);
43 support.firePropertyChange("", null, null); // there’s only one
44 }
45
46 public void keyPressed(KeyEvent e) { }
47
48 public void keyReleased(KeyEvent e) { }
49
50 public void addPropertyChangeListener(PropertyChangeListener pl) {
51 support.addPropertyChangeListener(pl);
52 }
53
54 public void removePropertyChangeListener(PropertyChangeListener pl) {
55 support.removePropertyChangeListener(pl);
56 }
57
58 private PropertyChangeSupport support = new
59 PropertyChangeSupport(this);
60 private OurButton target;
61 private TextField labelField;
62 }

JavaBeans Component Development Module 7, slide 33 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Recap of Customizers
Creating a Customizer Class

Define a BeanInfo file


public class MyBeanBeanInfo extends SimpleBeanInfo() {
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(MyBean.class, MyBeanCustomizer.class);
}
}

Extend java.awt.Component or a subclass and implement the Customizer interface


public class MyBeanCustomizer extends Panel implements Customizer {

Provide a null argument constructor for the customizer


public MyBeanCustomizer() {
//code here
}

Support the addition and removal of a PropertyChangeListener

private PropertyChangeSupport support = new


PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener pl) {


support.addPropertyChangeListener(pl);
}

public void removePropertyChangeListener(PropertyChangeListener pl) {


support.removePropertyChangeListener(pl);
}

Define the setObject(Object bean) method


public void setObject(Object beanToCustomize) {
private MyBean bean = (MyBean) beanToCustomize;
// remaining code here
}

JavaBeans Component Development Module 7, slide 34 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Creating a Customizer


• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 7, slide 35 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• What mechanisms are provided in the JavaBeans API
that enable customization of a Bean?

• Compare property sheets and property editors.

• Create a property editor for a specified property of a


Bean.

• Describe a situation where it would be beneficial to use


a customizer for a Bean.

• Create a customizer for a Bean component.

JavaBeans Component Development Module 7, slide 36 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
The next module on adaptors addresses some of the issues
that can occur when trying to handle events that an object has
registered for.

• For instance, if you register with several components


that send ActionEvents, how do you determine which
component the ActionEvent originated from?

• What about the case where an event source has several


interested objects that want tailored information from
the event sent by the source?

JavaBeans Component Development Module 7, slide 37 of 37


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 8

Event Adapters

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 8, slide 2 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Is an Event Adapter?


• Definition
• Diagram overview
Event
source
Interested
object
Fires (“listener”)

XXXEvent
XXXEvent

Event
adapter
(implements
XXXListener
interface; does
decision-making)

JavaBeans Component Development Module 8, slide 3 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Adapters Used in the BeanBox


• When are hookup classes created
• Where the generated code is stored
• Example of an adapter hookup class:
package tmp.sun.beanbox;

public class ___Hookup_13fdc34850 implements java.awt.event.ActionListener,


java.io.Serializable {
public void setTarget(sl292.choice.MyChoice t) {
target = t;
}

public void actionPerformed(java.awt.event.ActionEvent arg0) {


target.addRemoveItem(arg0);
}

private sl292.choice.MyChoice target;


}

JavaBeans Component Development Module 8, slide 4 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Types of Adapters
• Demultiplexing
• Multiplexing
• Common uses of adapters

JavaBeans Component Development Module 8, slide 5 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Adapter Diagrams
Event
source1 Fire
event
Forward
Event Fire Demultiplexing event Interested
source2 event adapter or call object
specific
method
Event Fire
source3 event

Interested
Fire object
event

Event Fire Multiplexing


source event Fire Interested
adapter event object

Fire
event Interested
object

JavaBeans Component Development Module 8, slide 6 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Differentiating Adapters From


Normal Listeners
• Case with no adapters
• Case with adapters

JavaBeans Component Development Module 8, slide 7 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Demultiplexing Adapter Example


• Application window:

• Description of application
• Appl.java
• Widgets.java
• ActionAdapter.java

JavaBeans Component Development Module 8, slide 8 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Appl.java Code
1
2 // An application that creates a Frame with several widgets that
3 // each generate an ActionEvent
4 // The ActionEvent is fired to the Actionadapter, which decides
5 // what widget the event originated, and calls an appropriate method
6
7 public class Appl {
8 public Widgets wid;
9
10 static public void main(String args[]) {
11 Appl app = new Appl();
12 app.wid = new Widgets(app);
13 }
14
15 public void apply() {
16 System.out.println("Do apply ...");
17 }
18
19 public void quit() {
20 System.out.println("Quitting...");
21 }
22
23 public void doFonts() {
24 System.out.println("put up Fonts dialog");
25 }
26
27 public void doColor() {
28 System.out.println("put up Color dialog");
29 }
30
31 public void doPatterns() {
32 System.out.println("put up Patterns dialog");
33 }
34 }

JavaBeans Component Development Module 8, slide 9 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Widgets.java Code
1 import java.awt.*;
2 import java.awt.event.*;
3
4 // Create the Widgets for this application
5 // and register the adapter as the listener for each
6
7 public class Widgets {
8 Button apply, quit;
9 List myList;
10
11 public Widgets(Appl app) {
12 Frame f = new Frame("My Widgets");
13 f.setLayout(new FlowLayout());
14 ActionAdapter adapt = new ActionAdapter(app);
15
16 //Build the widgets
17 apply = new Button("Apply");
18 quit = new Button("Quit");
19 myList = new List();
20 myList.add("Fonts");
21 myList.add("Color");
22 myList.add("Patterns");
23
24 f.add(apply);
25 f.add(quit);
26 f.add(myList);
27
28 // add adapter as listener for buttons, list
29 apply.addActionListener(adapt);
30 quit.addActionListener(adapt);
31 myList.addActionListener(adapt);
32
33 //put up Frame
34 f.pack();
35 f.setVisible(true);
36 }
37 }

JavaBeans Component Development Module 8, slide 10 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
ActionAdapter.java
Class
1 import java.awt.event.*;
2
3 // The demultiplexing adapter that receives ActionEvents from
4 // several sources, determines which type source sent the event
5 // and calls the appropriate method
6
7 public class ActionAdapter implements ActionListener {
8 private Appl app;
9
10 public ActionAdapter(Appl app) {
11 this.app = app;
12 }
13
14 public void actionPerformed(ActionEvent evt) {
15 Object x = evt.getSource();
16 if (x == app.wid.apply)
17 app.apply();
18 else if (x == app.wid.quit)
19 app.quit();
20 else if (x == app.wid.myList){
21 int index = app.wid.myList.getSelectedIndex();
22 if (index == 0)
23 app.doFonts();
24 else if (index == 1)
25 app.doColor();
26 else if (index == 2)
27 app.doPatterns();
28 else
29 return;
30 } else
31 // Unidentified source
32 System.out.println("Error: widget item not recog-
nized");
33 }
34 }

JavaBeans Component Development Module 8, slide 11 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Multiplexing Adapters
Overview of multiplexer exercise:
• Receives DataEvent from test button
• Generates DataTableEvent
• Fires DataTableEvent to:
• XAxis DataTableBean
• YAxis DataTableBean
• Table DataTableBean

JavaBeans Component Development Module 8, slide 12 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Overview of Multiplexer
Exercise

YAxis
Test button
fires DataEvent DataTableBean

Multiplexing
adapter

Table
DataTableBean

XAxis
DataTableBean

JavaBeans Component Development Module 8, slide 13 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Working With Adapters


• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 8, slide 14 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Define event adapter.

• Compare multiplexing and demultiplexing adapters.

• What are common uses of event adapters?

• Differentiate an event adapter from an event listener.

• Write an event adapter for a Bean component.

JavaBeans Component Development Module 8, slide 15 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
• How can Beans be used in a distributed systems?

• Can Beans be used as intelligent front-ends that


interface with network servers?

• How might this work with RMI, JDBC, and JavaIDL


APIs from JavaSoft?

JavaBeans Component Development Module 8, slide 16 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 9

Distributed Computing
With Beans

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 9, slide 2 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Motivating Scenarios for Distributed


Bean Programming
• Areas of familiarity
• Future Enterprise JavaBeans
• Applications for distributed Beans
• Workflow applications
• Server applications
• Agents

JavaBeans Component Development Module 9, slide 3 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
JavaSoft Distributed
Technologies
• JDBC
• RMI
• JavaIDL

Database Database
J protocol
server
JavaBeans D
component B
C

JavaBeans IIOP
CORBA
component
server

JavaBeans RMI
component
Java
Server

JavaBeans Component Development Module 9, slide 4 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Definition of JDBC
• JDBC API and drivers
Application
makes calls to JDBC

JDBC driver
implements API for
specific database

Database

JavaBeans Component Development Module 9, slide 5 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Overview of JDBC Architecture

DriverManager

Driver Driver

Connection Connection

Connection Statement

ResultSet

Statement Statement Statement

ResultSet ResultSet

JavaBeans Component Development Module 9, slide 6 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
BeanBox JDBC Bean
• JDBC SELECT Bean

JavaBeans Component Development Module 9, slide 7 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Advantages of DataBase-Related
Beans
• Value-add to end user
• No need to understand SQL syntax to obtain data

JavaBeans Component Development Module 9, slide 8 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Definition of RMI
• Overview diagram
Implementation
Class
Client Server

Application Object
Implementation

Stub rmic (compiler) Skeleton

Method Call

Method Results

JavaBeans Component Development Module 9, slide 9 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
BeanBox RMI Bean
• QuoteMonitorBean

QuoteMonitor
RMI Bean

Server
application

JavaBeans Component Development Module 9, slide 10 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

RMI Architecture Overview


• Description of how it works
• Graphical overview

Application Java RMI Client Java RMI Server

Stubs Skeletons

RMI System Remote Remote


Reference Layer Reference Layer

Transport Transport

JavaBeans Component Development Module 9, slide 11 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

RMI Architecture Overview


• Transport Layer
• Remote Reference Layer
• RMI stubs and skeletons
• rmic command
• rmiregistry application

JavaBeans Component Development Module 9, slide 12 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

RMI Exercise Code


• Exercise description
• Conceptual overview:
Server
Client

DataFactory
Constructs
DataEvent Get data information Creates
from data
retrieved
from server Data
instance

JavaBeans Component Development Module 9, slide 13 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Source Files Provided


• Interfaces: DataFactory.java, Data.java
• Implementations and server: DataFactoryImpl.java,
DataImpl.java, DataServer.java
• Conceptual overview of exercise source files:
DataClient.java DataServer.java

DataFactory.java DataFactoryImpl.java
(interface)
Data DataImpl.java
Data.java
(interface) instance

List of DataImpl IDs


% rmiregistry&
% java DataServer

JavaBeans Component Development Module 9, slide 14 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Data.java File
1 package sl292.rmilab;
2
3 import java.rmi.*;
4 import java.awt.Dimension;
5
6 public interface Data extends Remote{
7
8 public String getID() throws RemoteException;
9
10 public String getXLabel() throws RemoteException;
11
12 public String getYLabel() throws RemoteException;
13
14 public String [] getXAxis() throws RemoteException;
15
16 public String [] getYAxis() throws RemoteException;
17
18 public Dimension getSize() throws RemoteException;
19
20 public String [] [] getDataValues() throws RemoteException;
21 }

JavaBeans Component Development Module 9, slide 15 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

DataFactory.java File
1 package sl292.rmilab;
2
3 import java.rmi.*;
4
5 public interface DataFactory extends Remote{
6
7 public Data getData(String id) throws RemoteException;
8 public String [] getDataList() throws RemoteException;
9 public void addData(DataImpl dImpl) throws RemoteException;
10 }

JavaBeans Component Development Module 9, slide 16 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
DataImpl.java File
1 package sl292.rmilab;
2
3 import java.rmi.*;
4 import java.rmi.server.*;
5 import java.awt.Dimension;
6
7 public class DataImpl extends UnicastRemoteObject implements Data{
8
9 private String id, xLabel, yLabel;
10 private String [] xAxis, yAxis;
11 private String [][] dataValues;
12 private Dimension size;
13
14 public DataImpl(String id, String xLabel, String yLabel)
15 throws RemoteException {
16
17 this.id = id;
18 this.xLabel = xLabel;
19 this.yLabel = yLabel;
20 this.size = new Dimension (8,5);
21 xAxis = new String [size.height];
22 yAxis = new String [size.width];
23 dataValues = new String [yAxis.length] [xAxis.length];
24 }
25
26 public String getID() throws RemoteException {
27 return id;
28 }

JavaBeans Component Development Module 9, slide 17 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
DataImpl.java File
29 public String getXLabel() throws RemoteException {
30 return xLabel;
31 }
32
33 public String getYLabel() throws RemoteException {
34 return yLabel;
35 }
36
37 public String [] getXAxis() throws RemoteException {
38 return xAxis;
39 }
40
41 public String [] getYAxis() throws RemoteException {
42 return yAxis;
43 }
44
45 public Dimension getSize() throws RemoteException {
46 return size;
47 }
48
49 public String [] [] getDataValues() throws RemoteException {
50 return dataValues;
51 }
52
53 public void setXAxis(String [] sa1) {
54 xAxis = sa1;
55 }
56
57 public void setYAxis(String [] sa2) {
58 yAxis = sa2;
59 }
60
61 public void setSize(int width, int height) {
62 size = new Dimension(width, height);
63 }
64
65 public void setDataValues(String [] [] sa3) {
66 dataValues = sa3;
67 }
68 }

JavaBeans Component Development Module 9, slide 18 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
DataFactoryImpl.java
1 package sl292.rmilab;
2
3 import java.rmi.*;
4 import java.rmi.server.*;
5 import java.util.Vector;
6
7 public class DataFactoryImpl extends UnicastRemoteObject
8 implements DataFactory{
9
10 private Vector storage;
11
12 public DataFactoryImpl() throws RemoteException {
13 storage = new Vector(1,1);
14 }
15
16 public Data getData(String id) throws RemoteException {
17 for (int i = 0; i < storage.size(); i++) {
18
19 if (((DataImpl)storage.elementAt(i)).getID().equals(id)){
20 return (Data)storage.elementAt(i);
21 }
22 }
23 return null;
24 }
25
26 public String [] getDataList() throws RemoteException {
27 String [] names = new String [storage.size()];
28
29 for (int i = 0; i < storage.size(); i++) {
30 names[i] = ((DataImpl)(storage.elementAt(i))).getID();
31 }
32 return names;
33 }
34
35 public void addData(DataImpl dImpl) throws RemoteException {
36 storage.addElement(dImpl);
37 }
38
39 }

JavaBeans Component Development Module 9, slide 19 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Server Code Overview


• Description of what the DataServer application does
• Description of binding a DataFactoryImpl object to a
name in rmiregistry:
Naming.rebind("dataServer", dfi);

JavaBeans Component Development Module 9, slide 20 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

CORBA/JavaIDL (Optional)
• Definition of JavaIDL
• What is IDL
• CORBA and RMI

JavaBeans Component Development Module 9, slide 21 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Creating an RMI Client Bean


• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 9, slide 22 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Describe, in one paragraph, the JDBC architecture.

• How might a Bean component work with JDBC?

• Draw a diagram that illustrates the main concepts of the


Java RMI architecture.

• How would you implement a Java RMI Bean for a given


RMI application?

• Create Bean components to be used on the client side of


a Java RMI application.

JavaBeans Component Development Module 9, slide 23 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
You have been running and testing Bean components using
the BeanBox. The BeanBox is not considered an application
builder tool.

• Without such a tool, how do you use your Beans and


build an application that runs outside of the BeanBox?

JavaBeans Component Development Module 9, slide 24 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 10

Beans Outside the


BeanBox

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 10, slide 2 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Options for Building Beans


• Building a Bean from scratch
• Inheriting from a Bean to form a new Bean
• Subclassing a Bean and adding a BeanInfo file
• Composing a Bean from other Beans
• Instantiating a Bean from a serialized prototype

JavaBeans Component Development Module 10, slide 3 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Subclassing a Bean and Adding


BeanInfo
• public class ExplicitButton extends OurButton
{ }
• Add a BeanInfo to:
• Restrict visible properties
• Add icons
• Specify a customizer
• Limit visible events on Edit menu

JavaBeans Component Development Module 10, slide 4 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Composing a Bean From Other Beans


• Use Beans.instantiate() instead of new with Beans
• Composite Bean responsible for:
• Proper reading and writing of all Beans
• Restoring connections between Beans if loaded from
persisted state

JavaBeans Component Development Module 10, slide 5 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Customizing and Saving a
Bean
1 import java.beans.*;
2 import sunw.demo.molecule.*;
3 import java.io.*;
4
5 // Create the serialized file of molecule changed to Benzene
6 public class MyMolSer {
7 private static Molecule moleculeB;
8
9 public MyMolSer() {
10 ClassLoader cl = null;
11 try {
12 cl =
13 Class.forName("sunw.demo.molecule.Molecule").getClassLoader();
14 moleculeB = (Molecule)
15 Beans.instantiate(cl,"sunw.demo.molecule.Molecule");
16 moleculeB.setMoleculeName("benzene");
17 FileOutputStream fos = new
18 FileOutputStream("sunw/demo/molecule/moleculeSerFile.ser");
19 ObjectOutputStream outputStream = new ObjectOutputStream(fos);
20 outputStream.writeObject(moleculeB);
21 } catch ( Exception e) {
22 throw new Error(e.toString());
23 }
24 }
25 public static void main (String args[]) {
26 MyMolSer m = new MyMolSer();
27 }
28}

% java MyMolSer

JavaBeans Component Development Module 10, slide 6 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Restoring the Bean
1 import java.awt.Frame;
2 import java.awt.Component;
3 import java.beans.Beans;
4 import java.io.*;
5
6 // Read the serialized file and display the bean
7 public class BeanUnSer extends Frame {
8 private static Component myBean;
9 public BeanUnSer(String beanSerFile) {
10 super("Unserialized from: "+beanSerFile);
11 try {
12 myBean = (Component) Beans.instantiate(null,beanSerFile);
13 } catch ( Exception e) {
14 System.out.println("Error unserializing bean " +
15 beanSerFile+": "+e);
16 }
17 this.add(myBean);
18 this.setSize(300,300);
19 this.show();
20 }
21
22 public static void main (String args[]) {
23 BeanUnSer m = new BeanUnSer(args[0]);
24 }
25 }

% java BeanUnSer
sunw.demo.molecule.moleculeSerFile

JavaBeans Component Development Module 10, slide 7 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating Applets and Applications


With Beans
Done by:
• A builder tool
• Steps involved
• Coding by hand
• Using a scripting language

JavaBeans Component Development Module 10, slide 8 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Issues for Applets That Are Beans


• Handling parameters supplied by PARAM tags
• Catering to the life cycle of the Bean/applet
• Ensuring serialization works properly

JavaBeans Component Development Module 10, slide 9 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Delivering Your Beans


• A set of .class files
• How are JAR files used?
• JAR file review

JavaBeans Component Development Module 10, slide 10 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Using JAR Files in HTML


• APPLET tag and attributes:
• ARCHIVE
• OBJECT
• CODE
• CODEBASE

JavaBeans Component Development Module 10, slide 11 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Using JAR Files in HTML


• OBJECT tag and its attributes:
• DATA
• TYPE
• CLASSID
• CODETYPE
• HTML code example

JavaBeans Component Development Module 10, slide 12 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

JAR Files With a Digital Signature


• Definition
• Why use digital signatures?
• Comparing handwritten and digital signatures
• Digital signatures and Beans in JAR files

JavaBeans Component Development Module 10, slide 13 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Digital Signatures and Certificates


X.509 defines certificates to contain:
• Digital signature
• Version number
• Unique subject name
• Public key
• Issuer name
• Serial number
• Validity period

JavaBeans Component Development Module 10, slide 14 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Fitting the Pieces Together

Private key

Java key Signed JAR Verify


JAR file tool file signature

Certificate

• JavaServer Toolkit uses:


• SSLv3 support
• https
• Signed code
JavaBeans Component Development Module 10, slide 15 of 30
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Security and Cryptography


• Digital signatures adds a layer of security
• Encryption (JCA API) can add still another layer of
security
• J/Crypto

JavaBeans Component Development Module 10, slide 16 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Tools Are Involved?


• Java Security API
• JAR tool
• javakey utility
• Applications that can work with signatures and
certificates

JavaBeans Component Development Module 10, slide 17 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

For More Information


• The Javakey Security Tool – javakey at http://
java.sun.com/products/jdk/1.1/docs/tooldocs/
solaris/javakey.html
• Why should I care about X.509 certificates? at http://
java.sun.com/products/jdk/1.1/docs/guide/
security/cert2.html
• Java Security at http://java.sun.com/security/
codesign/index.html
• J/Crypto from Baltimore Technologies at
http://www.baltimore.ie/jcrypto.htm

JavaBeans Component Development Module 10, slide 18 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Loading and Instantiating a Bean


• Steps involved
• Coding possibilities:
• In an applet—applet class loader
• In an application—Bean or system class loader
• From a serialized stream

JavaBeans Component Development Module 10, slide 19 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The Class Class


• What it is
• What it does
• What it provides

JavaBeans Component Development Module 10, slide 20 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

The ClassLoader Class


• What it is
• What it can do

JavaBeans Component Development Module 10, slide 21 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Instantiating a Bean in an
Applet
• HTML code:
<APPLET
ARCHIVE="file:/home/BDK/jars/twoSimple.jar"
CODE="sl292.twoSimpleBeans.LabelChanger"
WIDTH=300
HEIGHT=100
></APPLET>

• Applet code:
1 package sl292.twoSimpleBeans;
2
3 import java.awt.Frame;
4 import java.awt.BorderLayout;
5 import java.applet.Applet;
6 import java.beans.*;
7 import java.io.*;
8
9 public class LabelChanger extends Applet {
10 public void init() {
11 MyButton myButton = null;
12 MyText myText = null;
13 try {
14 ClassLoader cl = this.getClass().getClassLoader();
15 myText = (MyText) Beans.instantiate(cl,
16 "sl292.twoSimpleBeans.MyText");
17 myButton = (MyButton) Beans.instantiate(cl,
18 "sl292.twoSimpleBeans.MyButton");
19 } catch (Exception e) {
20 throw new Error(e.toString());
21 }

JavaBeans Component Development Module 10, slide 22 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Instantiating a Bean in an Applet


22 myText.addLabelListener(myButton);
23 setLayout(new BorderLayout());
24 add("Center", myText);
25 add("South", myButton);
26 }
27
28 public static void main(String args[]) {
29 Frame f = new Frame("Label Changer Application");
30 LabelChanger labelChanger = new LabelChanger();
31 f.setLayout(new BorderLayout());
32 f.add("Center",labelChanger);
33 labelChanger.init();
34 f.setSize(300,100);
35 f.setVisible(true);
36 }
37
38 } // class LabelChanger

JavaBeans Component Development Module 10, slide 23 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Instantiating a Bean in an Application


• Using applet code if a main is provided
java sl292.twoSimpleBeans.LabelChanger

• Using a Bean in a JAR file


• What class loader will be used?

JavaBeans Component Development Module 10, slide 24 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
JarLoader Example
1 import java.awt.*;
2 import java.beans.*;
3 import java.io.*;
4 import sun.beanbox.*;
5
6 /**
7 * Usage: java JarBeanTest beanName jarFileName
8 * Where beanName is a fully qualified class like
9 * “sunw.demo.jelly.JellyBean”, or it is a serialized file named like:
10 * “serFiles.redJellyBean” for serFiles/redJellyBean.ser or
11 * often “sunw.demo.jelly.JellyBean” for sunw/demo/jelly/JellyBean.ser
12 * if the JAR package includes one.
13 *
14 * Note: This test program was written to test pulling stuff out of a
15 * JAR file, but it is important to note that because it’s an application
16 * anything within CLASSPATH will be found too. If the program were
17 * modified so jar files were optional then it could be made to look
18 * exclusivly at local and system files.
19 *
20 * Note also that this is very much an academic exercise because the
21 * AppletLoader already understands JARs with ARCHIVE tag and serialized
22 * objects with OBJECT tag. The BeanWrapper with BDK uses AppletLoader
23 */
24 public class JarBeanTest extends Frame {
25 JarLoader jl;
26 JarInfo ji;
27
28 public Component loadBeanFromJar(String beanClassName,
29 String jarFileName) {
30
31 try {
32 jl = new JarLoader(jarFileName);
33 if (jl == null) {
34 System.err.println("file name passed is not a valid JarFile");
35 return null;
36 }
37 } catch (Exception ex) {
38 System.err.println("caught an exception while trying to load "
39 +jarFileName);

JavaBeans Component Development Module 10, slide 25 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
JarLoader Example
40 ex.printStackTrace();
41 return null;
42 }
43 ClassLoader cl = jl.getLoader();
44 // Ok, loaded all the classes -- now, instantiate *the requested one*
45
46 ji = jl.loadJar();
47
48 try {
49 // We can either get an instance of one of the entities in the archive
50 // (not the right thing to do for a bean but interesting)
51 //return (Component) ji.getInstance(beanClassName);
52
53 // Or we can instantiate a specific bean from the archive
54 // This should work for both ser and class versions of a bean
55 return (Component) Beans.instantiate(cl,beanClassName);
56
57 } catch (Exception e) {
58 System.err.println("Error instantiating "+beanClassName);
59 e.printStackTrace();
60 return null;
61 }
62 }
63
64 public static void main (String args[]) {
65 JarBeanTest me = new JarBeanTest();
66
67 Component myBean = me.loadBeanFromJar(
68 args[0], // bean class name
69 args[1]); // jar file name
70
71 me.add(myBean);
72 me.pack();
73 me.show();
74 }
75 }

JavaBeans Component Development Module 10, slide 26 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Hooking Beans Together


• Instantiate the Beans using Beans.instantiate
• Register listener Beans with source Beans
sourceBean.addXXXListener(listenerBean)
• Register visual Beans with layout manager

JavaBeans Component Development Module 10, slide 27 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Exercise: Writing Applets or


Applications With Beans
• Objective
• Preparation
• Tasks
• Exercise summary

JavaBeans Component Development Module 10, slide 28 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Assess the use of the BeanBox as a minimal
environment for testing and using Beans.

• What do you need to do to create an applet or


application using existing Bean components?

• List and provide examples for the various scenarios for


building Beans.

• How can digital signatures be used to make an “official”


Bean?

• Create an applet that uses existing Bean components


and run it in the Applet Viewer.

JavaBeans Component Development Module 10, slide 29 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
You have seen in this module how Beans are built, how they
are assembled into applets and applications, and how they are
delivered. You have seen the specific connecting code that is
required to glue these Beans together.
In the next module, you will see a broader display of the sort
of tools that are becoming available to enable the use of Beans
in the business environment.

JavaBeans Component Development Module 10, slide 30 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 11

Business Environment
for JavaBeans

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives
• References

JavaBeans Component Development Module 11, slide 2 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

“Write once, run anywhere”


• Phrase associated with Java language
• What the phrase has become with JavaBeans:
“Write once, run anywhere, reuse everywhere”

• Enterprise JavaBeans

JavaBeans Component Development Module 11, slide 3 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Component-Based Software Review


• Business influences
• Major elements
• Components
• Containers
• Scripting

• Review of Services

JavaBeans Component Development Module 11, slide 4 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Bridging JavaBeans to Other


Component Models
• Other component models in the industry
• Portability to containers
• ActiveX Bridge
• JavaBeans Migration Assistant for ActiveX

JavaBeans Component Development Module 11, slide 5 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

InfoBus Technology
• InfoBus technology and JavaBeans
• InfoBus architecture overview
• Data producers
• Data consumers
• Data controllers

JavaBeans Component Development Module 11, slide 6 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Development Environments
• Integrated development environments (IDEs)
• Rapid application development (RAD)

JavaBeans Component Development Module 11, slide 7 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Visual Application Builder Tools


• What are they?
• Examples
• Java Workshop
• Java Studio
• Mojo
• Visual Café
• JBuilder
• VisualAge
• Additional tools

• Deciding on the tool for you

JavaBeans Component Development Module 11, slide 8 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Appraise how JavaBeans changes the “write once, run
everywhere” capability of the Java platform.
• Identify and describe the three major elements of component-based
software.
• Compare and contrast rapid application development (RAD) and
integrated development environment (IDE) software with regard
to component development.
• Describe how JavaBeans interface with other component models
such as ActiveX/OLE/COM and OpenDoc.
• Defend the use of application builder tools for Bean component
development.

JavaBeans Component Development Module 11, slide 9 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Think Beyond
• What do you do if your Beans need to run in an
environment that does not support JDK 1.1?

JavaBeans Component Development Module 11, slide 10 of 10


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Module 12

Transitional Beans

JavaBeans Component Development January 1999


Sun Educational Services

Module Overview
• Course map
• Relevance
• Objectives

JavaBeans Component Development Module 12, slide 2 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

What Are Transitional Beans?


• Definition
• Compatibility with JavaBeans FCS
• Browser support for JavaBeans

JavaBeans Component Development Module 12, slide 3 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Creating a Transitional Bean


• Marker classes
• Example
• Developer rules

JavaBeans Component Development Module 12, slide 4 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Example of Transitional
Beans
• Description

JavaBeans Component Development Module 12, slide 5 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

ButtonPushEvent Class
1 package demo;
2
3 /**
4 * JDK1.1 Event Model
5 * This class describes the event that gets generated when
6 * OurButton gets pushed.
7 */
8
9 public class ButtonPushEvent extends sunw.util.EventObject {
10 public ButtonPushEvent(java.awt.Component source) {
11 super(source);
12 }
13 }

JavaBeans Component Development Module 12, slide 6 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Listener Interface
1 package demo;
2
3 /**
4 * This interface describes the method that gets called when
5 * an OurButton gets pushed.
6 */
7
8 public interface ButtonPushListener extends sunw.util.EventListener {
9 public void push(ButtonPushEvent e);
10 }

JavaBeans Component Development Module 12, slide 7 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
OurButton Class
1 package demo;
2
3 /**
4 * The OurButton class is a very minimal Bean that simply extends the
5 * standard JDK 1.0.2 Button class to throw a Bean event.
6 */
7
8 import java.util.Vector;
9 import java.awt.*;
10
11 public class OurButton extends java.awt.Button implements
12 sunw.io.Serializable {
13 private boolean dbg;
14 private Vector listeners = new Vector();
15
16 public OurButton() {
17 this("press");
18 }
19
20 public OurButton(String label) {
21 super(label);
22 }
23
24 // Register an Event Listener.
25 public synchronized void addButtonPushListener(ButtonPushListener
26 bl) {
27 listeners.addElement(bl);
28 }
29// Remove an Event Listener.
30 public synchronized void removeButtonPushListener(ButtonPushListener
31 bl) {
32 listeners.removeElement(bl);
33 }

JavaBeans Component Development Module 12, slide 8 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
OurButton Class
34
35 // Catch an old style AWT event and fire it as a new style event.
36
37 public boolean handleEvent(Event evt) {
38 if (evt.id == Event.ACTION_EVENT) {
39
40 // Notify each of our listeners.
41 Vector l;
42 ButtonPushEvent e = new ButtonPushEvent(this);
43 synchronized(this) {
44 l = (Vector) listeners.clone();
45 }
46
47 for (int i = 0; i < l.size(); i++) {
48 ButtonPushListener bl =
49 (ButtonPushListener)l.elementAt(i);
50 bl.push(e);
51 }
52
53 }
54 return super.handleEvent(evt);
55 }
56
57 public boolean isDebug() {
58 return dbg;
59 }
60
61 public void setDebug(boolean x) {
62 dbg = x;
63 }
64 }

JavaBeans Component Development Module 12, slide 9 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
JellyBean Class
1 package demo;
2 /**
3 * A very simple example Bean.
4 *
5 * We have a screen appearance that is a color oval, and we have one
6 * property "color".
7 */
8
9 import java.awt.*;
10
11 public class JellyBean extends Canvas implements
12 sunw.io.Serializable {
13
14 public JellyBean() {
15 resize(60,40);
16 }
17
18 public void paint(Graphics g) {
19 g.setColor(ourColor);
20 g.fillArc(5, 5, 30, 30, 0, 360);
21 g.fillArc(25, 5, 30, 30, 0, 360);
22 g.fillRect(20, 5, 20, 30);
23 }
24
25 public Color getColor() {
26 return ourColor;
27 }
28
29 public void setColor(Color newColor) {
30 ourColor = newColor;
31 repaint();
32 }
33
34 private Color ourColor = Color.orange;
35 }

JavaBeans Component Development Module 12, slide 10 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services
Flipper Applet
1 package demo;
2
3 /**
4 * A very simple demo applet that uses the "OurButton" Bean
5 * and the "JellyBean" Bean.
6 *
7 * We catch "ButtonPush" events from the button and then change the
8 * color on the target JellyBean.
9 */
10
11 import java.awt.Color;
12
13 public class Flipper extends java.applet.Applet implements
14 ButtonPushListener {
15
16 public void init() {
17
18 // We do our own hand layout.
19 setLayout(null);
20
21 // Create a JellyBean.
22 jb = new JellyBean();
23 jb.setColor(firstColor);
24 add(jb);
25 jb.reshape(250, 25, 100, 40);
26
27 // Create a button.
28 btn = new OurButton();
29 btn.setLabel("flip");
30 add(btn);
31 btn.reshape(100, 30, 100, 30);
32

JavaBeans Component Development Module 12, slide 11 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Flipper Applet
33 // Add ourself as an event handler object for button pushes.
34 btn.addButtonPushListener(this);
35 }
36
37 /**
38 * This methods catches a button push event and uses it to
39 * flip the color off our JellyBean between two choices.
40 */
41
42 public void push(ButtonPushEvent evt) {
43 if (jb.getColor() == firstColor) {
44 jb.setColor(secondColor);
45 } else {
46 jb.setColor(firstColor);
47 }
48 }
49
50 private Color firstColor = Color.green;
51 private Color secondColor = Color.blue;
52 private JellyBean jb;
53 private OurButton btn;
54 }

JavaBeans Component Development Module 12, slide 12 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Things to Remember
• Define JDK 1.1 event X
• Define listener interface Y
• Define AWT 1.0.2 component as source for X
• Catch 1.0.2 events and generate event X
• Notify listeners
• Create JDK 1.1 listener for X

JavaBeans Component Development Module 12, slide 13 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4
Sun Educational Services

Check Your Progress


• Define transitional Beans.
• Explain when you might need to use transitional Beans.
• Describe the developer rules and marker classes used in
creating transitional Beans.
• Create a transitional Bean.

JavaBeans Component Development Module 12, slide 14 of 14


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services January 1999, Revision A.4

You might also like