copyright oracle corporation, 1998. all rights reserved. 3 javabeans: reusing application...

Post on 14-Dec-2015

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Copyright Oracle Corporation, 1998. All rights reserved.

33

JavaBeans: Reusing Application Components

JavaBeans: Reusing Application Components

Copyright Oracle Corporation, 1998. All rights reserved.3-3-22

ObjectivesObjectives

After completing this lesson, you should be able to do the following:

• Describe the JavaBeans architecture

• Identify the properties, methods, and events for a JavaBean

• Use JavaBeans provided in the JavaBeans Component Library (JBCL)

After completing this lesson, you should be able to do the following:

• Describe the JavaBeans architecture

• Identify the properties, methods, and events for a JavaBean

• Use JavaBeans provided in the JavaBeans Component Library (JBCL)

Copyright Oracle Corporation, 1998. All rights reserved.3-3-33

OverviewOverview

A JavaBean is a reusable Java component that conforms to a standard patternA JavaBean is a reusable Java component that conforms to a standard pattern

Public methods

Protected methods

Private methods

Data

EventsEvents

Methods and Methods and property accessorsproperty accessors

A JavaBean componentA JavaBean component

Copyright Oracle Corporation, 1998. All rights reserved.3-3-44

The aim is to be able to construct programs as a series of components

• Components can be visual or nonvisual

The aim is to be able to construct programs as a series of components

• Components can be visual or nonvisual

Aim of the Component ModelAim of the Component Model

Word Word processor processor programprogram

Thesaurus component

TextEditor component

SpellChecker component

Copyright Oracle Corporation, 1998. All rights reserved.3-3-55

Components in Java: JavaBeans

Components in Java: JavaBeans

Any Java class has the capability of being a JavaBean, such as ButtonControlAny Java class has the capability of being a JavaBean, such as ButtonControl

getLabel()label

setLabel()

requestFocus()

enable()

buttonControl1

• Methods• Methods

• Properties• Properties

• Events• Events

Copyright Oracle Corporation, 1998. All rights reserved.3-3-66

Assembling JavaBeans Components

Assembling JavaBeans Components

AppBuilder enables components to be plugged together using drag and drop

• Components have property sheets

• Design time and run timeconfiguration

AppBuilder enables components to be plugged together using drag and drop

• Components have property sheets

• Design time and run timeconfiguration

Copyright Oracle Corporation, 1998. All rights reserved.3-3-77

The JavaBean Component Library (JBCL)

The JavaBean Component Library (JBCL)

• AppBuilder provides many JavaBeans, in the JBCL

• Some of the JavaBeans are contained in the Component Palette

– Grouped by category

• AppBuilder provides many JavaBeans, in the JBCL

• Some of the JavaBeans are contained in the Component Palette

– Grouped by category

Copyright Oracle Corporation, 1998. All rights reserved.3-3-88

Anatomy of a JavaBean: GridControl Example

Anatomy of a JavaBean: GridControl Example

GridControl is a visible JBCL JavaBean

• Located in the Controls tab

• Defined in borland.jbcl.controlpackage

GridControl is a visible JBCL JavaBean

• Located in the Controls tab

• Defined in borland.jbcl.controlpackage

Copyright Oracle Corporation, 1998. All rights reserved.3-3-99

JavaBean PropertiesJavaBean Properties

Properties have “get” and “set” methodsProperties have “get” and “set” methods

// Possible implementation

public class GridControl … {

private int columnHeaderHeight;

public int getColumnHeaderHeight() {

return columnHeaderHeight;

}

public void setColumnHeaderHeight(int h) {

columnHeaderHeight = h;

} …

Height of column header (pixels)Height of column header (pixels)

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1010

JavaBean boolean Properties JavaBean boolean Properties

boolean property methods have a different name:boolean property methods have a different name:

// Possible implementation

public class GridControl … {

private boolean multiSelect;

public boolean isMultiSelect() {

return multiSelect;

}

public void setMultiSelect(boolean m) {

multiSelect = m;

} …

Allow multiple selectionAllow multiple selection

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1111

Accessing Properties at Run Time

Accessing Properties at Run Time

The get and set methods for a property can be called at run timeThe get and set methods for a property can be called at run time

public class MyClass {

public void myMethod() {

boolean b = gridControl1.isEnabled();

gridControl1.setEnabled(!b);

b = gridControl1.isColumnHeaderVisible();

gridControl1.setColumnHeaderVisible(!b);

}

}

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1212

Examining Properties in the Help System

Examining Properties in the Help System

The Help System defines properties for each componentThe Help System defines properties for each component

2

1

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1313

Property Editors for More Complex Properties

Property Editors for More Complex Properties

Property Editors can be provided to edit more complex properties at design timeProperty Editors can be provided to edit more complex properties at design time

Background colorBackground color

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1414

Guided Practice: Using Property Editors

Guided Practice: Using Property Editors

If a GridControl is added to an applet:

• How can you specify the items property?

• How is the items property defined in the GridControl class?

• How are the accessor methods defined?

If a GridControl is added to an applet:

• How can you specify the items property?

• How is the items property defined in the GridControl class?

• How are the accessor methods defined?

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1515

JavaBean MethodsJavaBean Methods

JavaBeans provide public methods, as for a regular classJavaBeans provide public methods, as for a regular class

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1616

Using JavaBean MethodsUsing JavaBean Methods

You can use JavaBean methods in your code, to manipulate the componentYou can use JavaBean methods in your code, to manipulate the component

public class MyClass {

public void myMethod() {

for (int r = 0; r < 3; r++) {

gc1.addRow();

gc1.addColumn();

}

for (int r = 0; r < gc1.getRowCount(); r++)

gc1.set(r, 0, "hello row " + r + " col 0");

}

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1717

JavaBean EventsJavaBean Events

• JavaBeans generate events to notify listeners when something important happens

• Can use existing event types, or define custom event types

• New event types extend EventObject

• JavaBeans generate events to notify listeners when something important happens

• Can use existing event types, or define custom event types

• New event types extend EventObject

XYZEvent

EventObject

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1818

Event ListenersEvent Listeners

For each type of event, there is an “event listener” interface

• Listeners must implement this interface

• JavaBean provides methods to add and remove listener objects

For each type of event, there is an “event listener” interface

• Listeners must implement this interface

• JavaBean provides methods to add and remove listener objects

Listener implements XYZListener

handler methodXYZEventXYZEvent

addXYZListeneraddXYZListener

JavaBean component

(event source)

Copyright Oracle Corporation, 1998. All rights reserved.3-3-1919

Examining Events in the Help SystemExamining Events in the Help System

The Help System defines the event listeners for each component

Notice themodelevent listener

The Help System defines the event listeners for each component

Notice themodelevent listener

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2020

JBCL Components Use the Model-View Architecture

JBCL Components Use the Model-View Architecture

“View” object defines visual appearance

“Model” object maintains the data

“View” object defines visual appearance

“Model” object maintains the data

Model type

Singleton

Vector

Matrix

Graph

Description

Single data item (used by text fields)

List of items (used by list controls)

Table of items (used by grid controls)

Tree of items (used by tree controls)

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2121

Dealing with Model Events Dealing with Model Events

GridControl generates model events when its data changes

Model contentchangedevent

GridControl generates model events when its data changes

Model contentchangedevent

ModelContentChangedModelContentChanged

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2222

Guided Practice: modelContentChanged Events

Guided Practice: modelContentChanged Events

What is the effect of this handler method?What is the effect of this handler method?

void gridControl1_modelContentChanged

(MatrixModelEvent e) {

MatrixModel model = e.getModel();

MatrixLocation location = e.getLocation();

int r = location.row;

int c = location.column;

System.out.println(" Row: " + r + " Col: " + c +

" Data: " + model.get(r, c));

}

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2323

Other JBCL ControlsOther JBCL Controls

JBCL includes a set of controls similar to the AWT controlsJBCL includes a set of controls similar to the AWT controls

AWT controls

Button

Checkbox

CheckboxGroup

Choice, List

Label

TextArea, TextField

Comparable JBCL controls

ButtonControl

CheckboxControl

CheckboxPanel

ChoiceControl, ListControl

LabelControl

TextAreaControl, TextFieldControl

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2424

AWT and JBCL Components AWT and JBCL Components

• Reasons to use AWT components:

– Standard Java classes

– Retain native appearance

– Less overhead

• Reasons to use JBCL components:

– Many are data aware

– More sophisticated

– Conform to model-view architecture

• Reasons to use AWT components:

– Standard Java classes

– Retain native appearance

– Less overhead

• Reasons to use JBCL components:

– Many are data aware

– More sophisticated

– Conform to model-view architecture

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2525

Other JBCL Components in the Controls Tab

Other JBCL Components in the Controls Tab

ButtonBarButtonBar

TabsetControlTabsetControl

TreeControlTreeControl

ImageControlImageControl

StatusBarStatusBar

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2626

JBCL Components in the Containers Tab JBCL Components

in the Containers Tab

TabsetPanelTabsetPanel

BevelPanelBevelPanel

SplitPanelSplitPanel

GroupBoxGroupBox

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2727

JBCL Components in the Dialogs Tab

JBCL Components in the Dialogs Tab

Representing standard dialog boxes:

•Filer, ColorChooser, FontChooser

Representing standard dialog boxes:

•Filer, ColorChooser, FontChooser

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2828

Guided Practice:Using Dialog Box Components

Guided Practice:Using Dialog Box Components

Consider that a Filer component has been added to a program

Describe the following code:

• How can a FontChooser be used?

Consider that a Filer component has been added to a program

Describe the following code:

• How can a FontChooser be used?

void aMethod() {

filer1.setMode(Filer.LOAD);

filer1.setFrame(new Frame());

filer1.show();

System.out.println(filer1.getFile());

}

Copyright Oracle Corporation, 1998. All rights reserved.3-3-2929

SummarySummary

• A JavaBean is a Java component

• AppBuilder provides many JavaBeans in the JBCL, including:

– Enhanced versions of AWT controls

– Containers

– Dialog boxes

– Data controls, the theme of the next two lessons

• A JavaBean is a Java component

• AppBuilder provides many JavaBeans in the JBCL, including:

– Enhanced versions of AWT controls

– Containers

– Dialog boxes

– Data controls, the theme of the next two lessons

Copyright Oracle Corporation, 1998. All rights reserved.3-3-3030

Practice 3-1 OverviewPractice 3-1 Overview

• Use the JBCL components to enhance the user interface

• Use JBCL property editors to fine tune component appearance and behavior

• Deal with JBCL events

• Work with the JBCL model-view architecture

• Use the JBCL components to enhance the user interface

• Use JBCL property editors to fine tune component appearance and behavior

• Deal with JBCL events

• Work with the JBCL model-view architecture

top related