Introduction to Java Beans CIS 421 Web-based Java Programming.

26
Introduction to Java Beans CIS 421 Web-based Java Programming

Transcript of Introduction to Java Beans CIS 421 Web-based Java Programming.

Page 1: Introduction to Java Beans CIS 421 Web-based Java Programming.

Introduction to Java Beans

CIS 421

Web-based Java Programming

Page 2: Introduction to Java Beans CIS 421 Web-based Java Programming.

Components

Components are :1. self-contained,

2. reusable

software units that can be visually composed into

1. composite components,

2. applets, and

3. applications

using visual application builder tools.

Page 3: Introduction to Java Beans CIS 421 Web-based Java Programming.

What are Component Models?

Component Models are the next step in object-oriented programming

Component Models allow us to:– Create objects as components – Customize a component – Introspect a component – Interact with other components – Build other higher level components

Page 4: Introduction to Java Beans CIS 421 Web-based Java Programming.

Desirable Properties of Components

A component should:

– be modular

– follow object oriented principles

– be “plug & play”

– be generic so that it can be used in different scenarios.

Page 5: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java & Components

Java is a good platform for component technology because:

– It is object-oriented

– It provides applets that run on a web server.

– It has platform independence.

– It has basic features like multi threading, security, etc.

– It has features to communicate with remote objects.

– It is able to furnish info on class contents at run-time.

Page 6: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Beans

A Java Bean is a reusable software component that can be manipulated visually in a builder tool– BeanBox is a simple tool

Note: Not all beans are visual beans.

Page 7: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Beans contd..

Features that distinguish a Java Bean from Java objects are:

– introspection

– customization

– externally occurring events

– properties

– persistence

Page 8: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Beans contd..

A bean is not required to inherit from any particular base class or interface.

Visual beans inherit from java.awt.Component.

Though Beans are primarily targeted at builder tools, they are also entirely usable by human programmers.

Beans are more than class libraries.– ordinarily stored in jar files

Page 9: Introduction to Java Beans CIS 421 Web-based Java Programming.

Properties, Methods and Events

Properties are the named attributes associated with a bean.

Methods of a Bean are the normal Java methods which can be called from other components.

Events are a way of providing information to other components by notifying them about something which happened.

Page 10: Introduction to Java Beans CIS 421 Web-based Java Programming.

Design Time Vs. Run Time

Design Environment Provides Information used by builder tool to customize

the appearance and behavior of Beans.

Run-time Environment Provides information for other beans as well as

applications to use it.

Java Beans API allows design time interfaces to be separated from run-time interfaces.

Page 11: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Features used in Beans

Beans can be stored and retrieved for a later use by implementing Java’s Serializable interface. – Remote Invocation

Java RMI provides a facility for remote Beans to communicate with each other.

JDBC Connectivity – JDBC provides a means to store Beans in a database.

Page 12: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Features used in Beans contd..

JAR Utility– Used for packaging beans

AWT and Swing– All visible beans are made from AWT or Swing components. – Beans use event handling of AWT.

Reflection– Used to know about the beans at run-time.

Security - Beans follows the same security model of Java.

Page 13: Introduction to Java Beans CIS 421 Web-based Java Programming.

Design Pattern

Beans follow strict design patterns.

Design pattern helps IDEs to detect various state variables for configuration and editing.

The common design patterns are: Set / Get methods for properties Add / Remove methods for events

Page 14: Introduction to Java Beans CIS 421 Web-based Java Programming.

Event-driven Designs

Handling user interface events :

-Mouse Actions

-Keyboard events

Managing / reporting inter-client connections.

Other events: Property changes in a bean Any general-purpose notification

Page 15: Introduction to Java Beans CIS 421 Web-based Java Programming.

Delegation Event Model

Based on publish and subscribe system .

Objects that provide events are called publishers or sources.

Objects that subscribe, and can receive events, are listeners or targets.

Sources fire events and listeners wait for event to be

fired on them.

Page 16: Introduction to Java Beans CIS 421 Web-based Java Programming.

Delegation Event Model [1] contd..

Public synchronized FooListener addFooListener (FooListener fel)

Event SourceFooEvent

Event Listener

Class fooey implements FooListner {Void fooBarHappened (FooEvent fe) { }

}

eListener

Interface reference

fire Event

Register Listener

Page 17: Introduction to Java Beans CIS 421 Web-based Java Programming.

Event Handling in AWT

A listener object is an instance of a class that implements a special interface called listener interface.

An event source is an object that can register listener objects and send them event objects.– addActionListener(this)

The event source sends out event objects to all registered listeners when that event occurs.

The listener objects will then use the information in the event object to determine their reaction to the event.

Page 18: Introduction to Java Beans CIS 421 Web-based Java Programming.

Java Event Libraries

java.util.eventObject

java.awt.event

java.awt.dnd

javax.swing.event

Page 19: Introduction to Java Beans CIS 421 Web-based Java Programming.

Types of Events

Low-level events:– Tied to GUI component like focus, click, key-pressed, etc.

Semantic events:

Used in a higher level where the user can customize the way of representing event.

Page 20: Introduction to Java Beans CIS 421 Web-based Java Programming.

Low-level Events

EventObject

CompoentEvent

FocusEvent InputEvent

MouseEvent

Page 21: Introduction to Java Beans CIS 421 Web-based Java Programming.

Semantic Events

ActionEvent ItemEvent

AdjustmentEvent

Page 22: Introduction to Java Beans CIS 421 Web-based Java Programming.

Event Sources Sources are the objects which fire events. Sources maintain a list of listeners.

– keep track of added listeners Listeners can register and unregister themselves with a s

ource. Listener registration methods are prepended by add and

remove.

Event Listeners waits for events. Listener names end with Listener

Page 23: Introduction to Java Beans CIS 421 Web-based Java Programming.

User-defined events?

Custom Events:– Can create and define events.

User defined event objects extend the EventObject class.

Page 24: Introduction to Java Beans CIS 421 Web-based Java Programming.

Event Delivery

Events can be delivered as either unicast or multicast.

– Unicast – Only one listener possible.Throws ToomanyListenersException

– Multicast – Many listeners possible.

Page 25: Introduction to Java Beans CIS 421 Web-based Java Programming.

Adding an Event to the Bean

Create EventListener class.

Create EventListener objects in the bean.

Register the EventListener objects with Swing or AWT components.

Page 26: Introduction to Java Beans CIS 421 Web-based Java Programming.

Event Adapter

Event adaptors decouple the incoming event notifications from the listeners.

Event adaptors are placed in between source and the actual listeners to provide an additional policy in event delivery.

Typical use of event adapters are:– Implementing an event queuing mechanism between sources and listen

ers.

– Acting as a filter.

– Demultiplexing multiple event sources onto a single event listener.