All this material assumes you have some familiarity with using JBuilder and its standard components.
For more fast-track information about building JavaBeans components, see Creating JavaBeans with BeansExpress.
When we refer to users in this book, we are not referring to the users of the applications written by component users, but to the component users themselves.
There are important differences between creating a component for use in JBuilder and the more common task of creating an application or applet that uses components:
Because of these differences, you must be aware of the rules of the JavaBeans component model, and you must think about how other programmers will use the components you write.
When extending an existing class to create a new one, you have access to protected class members, parts of the superclass unavailable to end users of that same class. Subclasses also must call on their superclasses for a lot of their implementation, so component writers must be familiar with inheritance and delegation.
Try to anticipate how your component might be used. You want to make your component useful in many situations and you want it to be error free.
When designing components, keep in mind the interaction of properties. Ensure that a user can set properties in any order, or to any combination of values, without the component failing. Constrain properties, when necessary, to a given set of acceptable values (enumerated properties). Design events and methods to function independently of their order of occurrence at runtime. The general rule is that no element of a component should depend upon another event, value, or method call to function.
Your components should have everything they need to function built in. If a component always needs a certain helper class or companion non-UI component, it is up to the component writer to develop those additional objects.
For example, the TreeView and TreeControl components in the JavaBeans Component Library require a tree node class to function. The general tree node class (LinkedTreeNode) has been developed and delivered in another library package (borland.jbcl.model). The component user who needs a TreeControl component does not need to reinvent the node class to use the component.
Example
A new software project might require blue backgrounds on all of the
dialog boxes in the application. Instead of relying on all members of the
development team to change the background color on every dialog box they
create, you can extend BevelPanel and change the default background color. Once installed as a new component, the blue panel would be available to all developers with no extra work on their part.
The following code declares the new panel component, with the new default background color setting.
import java.awt.*; public class BluePanel extends BevelPanel { public BluePanel() { super(); setBackground(Color.blue); //Sets the background color to blue. } }
Once compiled and tested, BluePanel is ready for use by the team of developers.
UI and non-UI components follow the same rules. They are public classes with a public constructor that takes no parameters. You write their properties, methods, and events exactly as you do those of a UI component.
Note
Component users work differently with non-UI components. Although a
non-UI component is selected from the Component Palette and dropped on
the UI Designer exactly as a UI component is, non-UI components never
display in the UI Designer. JBuilder adds the non-UI component to the Component
Tree.
To work with a non-UI component using JBuilder's environment, select the component on the Component Tree. The component's properties and events appear in the Component Inspector.
Each composite component needs
A model object stores the data and delivers the data items to the rest of the component. In the JavaBeans Component Library, model objects implement interfaces that provide access to the data. These interfaces can provide read-only (model interfaces) or read-write (writable model interfaces) access to the data.
A view of the component holds its essential behavior, the special things the component is constructed to do. It is the backbone of a model-view component. When you think of a control, it is the view that you are imagining. The view uses the model to obtain its the data. It passes the data along to the item painter for rendering to the screen.
Because model-view component architecture practices data hiding, and the view knows nothing about the data types it passes back and forth, it must ask a view manager for an appropriate item painter to render the data. The view manager has
You work programmatically with these elements, instantiating those appropriate to your component and writing custom elements as needed for your special cases. Many general-use elements are available as part of the JavaBeans Component Library (JBCL). Understanding model-view architecture discusses this way of constructing components in detail.
Before you begin creating your component, consider what the its primary attributes should be. Will your component contain other components? Do you want it to be transparent so that other components behind it show through, or do you want it to be opaque so that it completely covers other components? Do you want a lightweight component?
For information about lightweight versus heavyweight components, see Lightweight components.
JBuilder offers several components you can use as starting points for building new UI components. The following table lists the classes available to you:
com.sun.java.swing.JPanel | An opaque, lightweight visual Swing JavaBean container |
com.sun.java.swing.JComponent | A transparent, lightweight visual Swing JavaBean container |
borland.jbcl.view.BeanPanel | A transparent, lightweight visual Swing/JBCL JavaBean container with extra code for event dispatching, texture mapping, and focus handling |
borland.jbcl.control.BevelPanel | A borland.jbcl.view.BeanPanel with beveled edges |
java.awt.Panel | A heavyweight visual JavaBean container |
java.awt.Canvas | A heavyweight visual JavaBean that is not a container |
java.awt.Container | A lightweight, transparent visual JavaBean container |
java.awt.Component | A lightweight, transparent visual JavaBean that is not a container |
To install a completed component,
The Palette Properties dialog box appears.
If you are still developing your component and haven't placed the classes into a .JAR or .ZIP file yet, use the Add from Package page.
If you deployed your component as a .JAR or .ZIP file, use the Add from Archive page.
To specify an image for your component on the Component Palette with the Palette Properties dialog box,
To specify a new page in the Component Palette,
When you are through using the Palette Properties dialog box, the new page appears on the Component Palette.