Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1998. All rights reserved. 3 JavaBeans: Reusing Application Components.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1998. All rights reserved. 3 JavaBeans: Reusing Application Components."— Presentation transcript:

1 Copyright  Oracle Corporation, All rights reserved. 3 JavaBeans: Reusing Application Components

2 Copyright  Oracle Corporation, All rights reserved. 3-2 Objectives 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)

3 Copyright  Oracle Corporation, All rights reserved. 3-3 Overview A JavaBean is a reusable Java component that conforms to a standard pattern Public methods Protected methods Private methods Data Events Methods and property accessors A JavaBean component

4 Copyright  Oracle Corporation, All rights reserved. 3-4 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 Model Word processor program Thesaurus component TextEditor component SpellChecker component

5 Copyright  Oracle Corporation, All rights reserved. 3-5 Components in Java: JavaBeans Any Java class has the capability of being a JavaBean, such as ButtonControl getLabel() label setLabel() requestFocus() enable() buttonControl1 Methods Properties Events

6 Copyright  Oracle Corporation, All rights reserved. 3-6 Assembling JavaBeans Components AppBuilder enables components to be plugged together using drag and drop Components have property sheets Design time and run time configuration AppBuilder enables components to be plugged together using drag and drop Components have property sheets Design time and run time configuration

7 Copyright  Oracle Corporation, All rights reserved. 3-7 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

8 Copyright  Oracle Corporation, All rights reserved. 3-8 Anatomy of a JavaBean: GridControl Example GridControl is a visible JBCL JavaBean Located in the Controls tab Defined in borland.jbcl.control package GridControl is a visible JBCL JavaBean Located in the Controls tab Defined in borland.jbcl.control package

9 Copyright  Oracle Corporation, All rights reserved. 3-9 JavaBean Properties Properties 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)

10 Copyright  Oracle Corporation, All rights reserved JavaBean boolean Properties 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 selection

11 Copyright  Oracle Corporation, All rights reserved Accessing Properties at Run Time The 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); }

12 Copyright  Oracle Corporation, All rights reserved Examining Properties in the Help System The Help System defines properties for each component 2 1

13 Copyright  Oracle Corporation, All rights reserved Property Editors for More Complex Properties Property Editors can be provided to edit more complex properties at design time Background color

14 Copyright  Oracle Corporation, All rights reserved 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?

15 Copyright  Oracle Corporation, All rights reserved JavaBean Methods JavaBeans provide public methods, as for a regular class

16 Copyright  Oracle Corporation, All rights reserved Using JavaBean Methods You 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"); }

17 Copyright  Oracle Corporation, All rights reserved JavaBean 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

18 Copyright  Oracle Corporation, All rights reserved Event 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 method XYZEvent addXYZListener JavaBean component (event source)

19 Copyright  Oracle Corporation, All rights reserved Examining Events in the Help System The Help System defines the event listeners for each component Notice the model event listener The Help System defines the event listeners for each component Notice the model event listener

20 Copyright  Oracle Corporation, All rights reserved 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)

21 Copyright  Oracle Corporation, All rights reserved Dealing with Model Events GridControl generates model events when its data changes Model content changed event GridControl generates model events when its data changes Model content changed event ModelContentChanged

22 Copyright  Oracle Corporation, All rights reserved Guided Practice: modelContentChanged Events 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)); }

23 Copyright  Oracle Corporation, All rights reserved Other JBCL Controls JBCL 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

24 Copyright  Oracle Corporation, All rights reserved 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

25 Copyright  Oracle Corporation, All rights reserved Other JBCL Components in the Controls Tab ButtonBar TabsetControl TreeControl ImageControl StatusBar

26 Copyright  Oracle Corporation, All rights reserved JBCL Components in the Containers Tab TabsetPanel BevelPanel SplitPanel GroupBox

27 Copyright  Oracle Corporation, All rights reserved JBCL Components in the Dialogs Tab Representing standard dialog boxes: Filer, ColorChooser, FontChooser Representing standard dialog boxes: Filer, ColorChooser, FontChooser

28 Copyright  Oracle Corporation, All rights reserved 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()); }

29 Copyright  Oracle Corporation, All rights reserved Summary 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

30 Copyright  Oracle Corporation, All rights reserved Practice 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

31 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

32 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

33 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

34 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

35 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

36 Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices


Download ppt "Copyright  Oracle Corporation, 1998. All rights reserved. 3 JavaBeans: Reusing Application Components."

Similar presentations


Ads by Google