For more fast-track information about building JavaBeans components, see 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:
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.
Component creation starting points
To do this | Start with this type |
---|---|
Modify an existing component | Any component |
Create a non-UI component | Any class or none |
Create a composite component from model-view elements | Any existing view, instantiating a choice of existing or custom models and item painters. Alternatively, extend java.awt.Canvas or java.awt.Panel , and create a custom view. |
Create an entirely new UI component | java.awt.Canvas, java.awt.Panel, java.awt.Frame borland.jbcl.BevelPanel, or borland.jbcl.BeanPanel |
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.
If you need more flexibility than extending existing controls provides, you can compose a new component by constructing and connecting the smaller building blocks called models, views, view managers, item painters, and item editors. Components assembled this way are called composite components.
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
One or more item painters. One painter is needed for each data type the component paints to the screen.
One or more item editors if the component changes the data as well as views it. One editor is needed for each data type the component edits.
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.
Any time that you modify and recompile a component that is on the Component Palette, it is safest to restart JBuilder to ensure your modified class is loaded into memory.
Each component on the Component Palette has an image to represent it. If you don't specify an image for the component, the default JBuilder component icon appears. If you want to specify a unique image for your component, you can specify the icon in the BeanInfo class of your component. See Specifying component information with BeanInfo classes. Or you can use the Palette Properties dialog box to specify an image. The image that represents a component on the Component Palette must
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.