1 / 30

JavaBeans: Reusing Application Components

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).

metta
Download Presentation

JavaBeans: Reusing Application Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaBeans: Reusing Application Components

  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)

  3. 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

  4. 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

  5. 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

  6. Assembling JavaBeans Components • AppBuilder enables components to be plugged together using drag and drop • Components have property sheets • Design time and run timeconfiguration

  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

  8. Anatomy of a JavaBean: GridControl Example • GridControl is a visible JBCL JavaBean • Located in the Controls tab • Defined in borland.jbcl.controlpackage

  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. 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. 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. Examining Properties in the Help System • The Help System defines properties for each component 1 2

  13. Background color Property Editors for More Complex Properties • Property Editors can be provided to edit more complex properties at design time

  14. 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?

  15. JavaBean Methods • JavaBeans provide public methods, as for a regular class

  16. 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. 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

  18. 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

  19. Examining Events in the Help System • The Help System defines the event listeners for each component • Notice themodelevent listener

  20. 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)

  21. Dealing with Model Events • GridControl generates model events when its data changes • Model contentchangedevent ModelContentChanged

  22. 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. 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. 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

  25. Other JBCL Components in the Controls Tab ButtonBar TabsetControl ImageControl TreeControl StatusBar

  26. JBCL Components in the Containers Tab TabsetPanel BevelPanel GroupBox SplitPanel

  27. JBCL Components in the Dialogs Tab • Representing standard dialog boxes: • Filer, ColorChooser, FontChooser

  28. 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()); }

  29. 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

  30. 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

More Related