Top Banner
Java Beans
24

Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Java Beans

Page 2: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Definitions

A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification)The JavaBeans API provides a framework for defining reusable, embeddable, modular software components.

Page 3: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Intro to JavaBeans

What are JavaBeans? Software components written in Java Connect and Configure Components Builder Tools allow connection and

configuration of Beans Begins ‘Age of Component Developer’ Bringing Engineering methods to

Software Engineering (e.g. electronics…)

Page 4: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

The JavaBeans API

Features implemented as extensions to standard Java Class LibraryMain Component Services GUI merging Persistence Event Handling Introspection Application Builder Support

Page 5: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

User Interface Merging

Containers usually have Menus and/or toolbarsAllows components to add features to the menus and/or toolbarsDefine mechanism for interface layout between components and containers

Page 6: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Persistence

Components can be stored and retrievedDefault – inherit serializationCan define more complex solutions based on needs of the components

Page 7: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Event Handling

Defines how components interactJava AWT event model serves as basis for the event handling API’sProvides a consistent way for components to interact with each other

Page 8: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Introspection

Defines techniques so components can expose internal structure at design timeAllows development tools to query a component to determine member variables, methods, and interfacesStandard naming patterns usedBased on java.lang.reflect

Page 9: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Application Builder Support

Provides support for manipulating and editing components at design timeUsed by tools to provide layout and customizing during designShould be separate from componentNot needed at run time

Page 10: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Creating a JavaBean

Requirements for a simple BeanPackaging Bean in a JAR fileAdditional Information – BeanInfoDefining property editorsDefining Bean customizersNaming Conventions

Page 11: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Bean NON Requirements

No Bean SuperclassVisible interface not required ‘Invisible’ Beans (timer, random

number generator, complex calculation)

Page 12: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Bean RequirementsIntrospection Exports: properties, methods, events

Properties Subset of components internal state

Methods Invoked to execute component code

Events (If any needed) Notification of a change in state User activities (typing, mouse actions,

…)

Page 13: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Bean Requirements

Customization Developer can change appearance

Persistence Save current state so it can be

reloaded

Page 14: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Other properties

Indexed properties Array value with get and set elements

Bound properties Triggers event when value changed

Constrained properties Triggers event when value changes

and allows listeners to ‘veto’ the change

Page 15: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

BeanInfo class

Provides more information using FeatureDescripter objectsSubclasses: BeanDescripter, PropertyDescripter,

IndexedPropertyDescripter, EventSetDescripter, MethodDescripter, ParameterDescripter

Page 16: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

BeanInfo class

ICON to represent BeanCustomizer Class (wizard for set up)Property Editor referencesList of properties with descriptionsList of methods with descriptionsMethod to reset properties to defaults

Page 17: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

The beanbox

Primary task is setting property valuesProperty editors for common types Set Font Set background/foreground colors Set numeric values Set string values

Page 18: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Creating a Bean

Usually extends Canvas (New window)Can extend Component (‘lightweight’)Needs constructor with no argumentsPaint() method used to displaygetPreferredSize(), getMinimumSize() For layout manager defaults

get and set methods for each property

Page 19: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Packaging the Bean

Create a JAR file (JavaARchive) Patterned after tar utility

Create ‘stub’ manifest Name:

smith/proj/beans/BeanName.class Java-Bean: True (forward slashes even under

Windows!)

Page 20: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Installing the Bean

Beanbox: copy jar file to /jars directory within the BDK directory

Different depending on tool used

Page 21: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Some Naming Conventions

Beans Class name: any Constructor: no argument or

serialized template file Packaging: jar file with Java-Bean:

True

Page 22: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

More Naming Conventions

Properties Get and set using property name Property name: message

public String getMessage() Public void setMessage(String s)

Page 23: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

More Naming Conventions

Events Event name: Answer

Class name: AnswerEvent Listener name: AnswerListener Listener methods:

public void methodname(AnswerEvent e) public void addAnswerListener(AnswerListener

l) public void removeAnswerListener(… l)

Page 24: Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.

Demo of IBM’s VisualAge Tool

VisualAge for Java Version 1.0