300 likes | 462 Views
JavaBeans: Reusing Application Components. 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).
E N D
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)
Overview • A JavaBean is a reusable Java component that conforms to a standard pattern A JavaBean component Methods and property accessors Public methods Data Private methods Events Protected methods
SpellChecker component Aim of the Component Model • The aim is to be able to construct programs as a series of components • Components can be visual or nonvisual TextEditor component Word processor program Thesaurus component
Components in Java: JavaBeans • Any Java class has the capability of being a JavaBean, such as ButtonControl requestFocus() • Methods enable() getLabel() • Properties label setLabel() • Events buttonControl1
Assembling JavaBeans Components • AppBuilder enables components to be plugged together using drag and drop • Components have property sheets • Design time and run timeconfiguration
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
Anatomy of a JavaBean: GridControl Example • GridControl is a visible JBCL JavaBean • Located in the Controls tab • Defined in borland.jbcl.controlpackage
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)
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
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); } }
Examining Properties in the Help System • The Help System defines properties for each component 1 2
Background color Property Editors for More Complex Properties • Property Editors can be provided to edit more complex properties at design time
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?
JavaBean Methods • JavaBeans provide public methods, as for a regular class
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"); }
EventObject XYZEvent 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
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 addXYZListener JavaBean component (event source) Listener implements XYZListener XYZEvent handler method
Examining Events in the Help System • The Help System defines the event listeners for each component • Notice themodelevent listener
JBCL Components Use the Model-View Architecture • “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)
Dealing with Model Events • GridControl generates model events when its data changes • Model contentchangedevent ModelContentChanged
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)); }
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
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
Other JBCL Components in the Controls Tab ButtonBar TabsetControl ImageControl TreeControl StatusBar
JBCL Components in the Containers Tab TabsetPanel BevelPanel GroupBox SplitPanel
JBCL Components in the Dialogs Tab • Representing standard dialog boxes: • Filer, ColorChooser, FontChooser
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? void aMethod() { filer1.setMode(Filer.LOAD); filer1.setFrame(new Frame()); filer1.show(); System.out.println(filer1.getFile()); }
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
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