Creating a property editor
A property editor is an editor for changing property values at design time. You can see several different types of property editors in JBuilder’s Component Inspector. For example, for some properties, you simply type in a value in the Component Inspector, and by so doing, change the value of the property. This is the simplest type of property editor. For other properties, you use a choice menu (drop-down list) to display all the possible values and you select the value you want from that list. Colors and fonts have property editors that are actually dialog boxes you can use to set their values.
The JavaBeans Component Library supplies a few property editors for the primitive Java data types. When you create your own JavaBeans, you might create new property classes and want to have editors capable of editing their values. BeansExpress can help you create property editors that display a list of choices.
To begin creating a property editor for a property,
- Click the Editors tab in BeansExpress.
- Click the Create Custom Editor button.
The New Property Editor dialog box appears.
- Specify the name of your property editor in the Editor Name box.
Give the property editor the same name as the property it will edit with the word Editor appended. For example, if the property name is favoriteFoods, name the editor FavoriteFoodsEditor.
- Select the type of editor you want from the Editor Type drop-down list.
The appearance of the New Property dialog box changes depending on which type of editor you selected. The next four sections describe how to create a property editor of the four different types.
Creating a String List editor
A String List is a simple list of Strings. It appears in the Component Inspector as a drop-down list containing the strings you specify. When the user uses the list to select an entry, the property being edited is set to the selected value.
To add items to a String List editor,
- Choose Add Entry for each item you want to appear in the list.
- In each entry, enter the string you want to appear.
This is how the resulting New Property Editor dialog box might look:
- Choose OK, and a new property editor class is created.
To see the generated code,
- Select your property editor class in the Navigation pane.
- Click the Source tab.
Creating a String Tag List editor
A property editor that is a String Tag List also presents a list of strings to the user in the Component Inspector. When the user selects one of the items, the specified Java initialization string is used to set the property. A Java initialization string is the string JBuilder uses in the source code it generates for the property.
To add items to a String Tag List editor,
- Choose Add Entry for each item you want to appear in the list.
- In each entry, enter the string you want to appear and it’s associated Java initialization string.
If you want to include a string in your list of Java initialization strings, put quotation marks (“) before and after the string, as if you were entering it in source code.
Here is an example of how the dialog box might look:
- Choose OK, and a new property editor class is created.
To see the generated code,
- Select your property editor class in the Navigation pane.
- Click the Source tab.
Creating an Integer Tag List editor
An Integer Tag List property editor can be used to edit integer properties. It presents a list of strings to
the user in the Component Inspector, and when the user selects one of the items, the specified Java initialization
string is used to set the property.
To add items to an Integer Tag List editor,
- Choose Add Entry for each item you want to appear in the list.
- In each entry, enter the string you want to appear and its associated integer value and Java initialization
string.
If you want to include a string in your list of Java initialization strings, put quotation marks (“) before
and after the string, as if you were entering it in source code.
Here is an example of how the dialog box might look:
- Choose OK, and a new property editor class is created.
To see the generated code,
- Select your property editor class in the Navigation pane.
- Click the Source tab.
Creating a custom component property editor
You can also use your own custom component to edit the value of a property. Selecting this choice generates a skeleton property editor that uses the custom editor you specify to actually edit the property.
To specify a custom component property editor,
- Enter the name of your custom component in the Custom Editor Name box.
- Check the Support paintValue() option if your custom editor paints itself.
To see the generated code,
- Select your property editor class in the Navigation pane.
- Click the Source tab.
For more information about creating property editors, see Writing property editors in the Component Writer’s Guide.
Adding support for serialization
Serializing a bean saves its state as a sequence of bytes that can be sent over a network or saved to a file. BeansExpress can add the support for serialization to your class.
To add support for serialization,
- Select your bean in the Navigation pane.
- Click the Bean tab to display the BeansExpress designers.
- Click the General tab to display the General page.
- Check the Support Serialization option.
BeansExpress modifies the class so that it implements the Serializable interface. Two methods are added to the class: readObject() and writeObject():
void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
}
You must fill in these two methods to serialize and deserialize your bean.
For more information about serializing a bean, see Serializing JavaBeans in the Component Writer’s Guide.
Checking the validity of your bean
When you’re finished with your bean, you can use BeanInsight to verify that the component you created is a valid JavaBean. If your class fails as a JavaBean, BeanInsight reports why it failed. It identifies all the properties, customizers, and property editors it finds for the class. It also reports whether the property information is obtained through a BeanInfo class or through introspection.
To verify that your Java class is a JavaBean,
- Choose Wizards|BeanInsight to display BeanInsight:
- Type in the name of the component you want examined or use the Select Bean button to specify the component.
- Click the Examine Bean button to begin the examination process.
BeanInsight reports a summary of its findings in the BeanInsight Results section of the wizard.
- To see complete details on BeanInsight’s findings, click the View Details button.
- Click the various tabs to see the full report.
Installing your bean on the Component Palette
Once you have built a valid JavaBeans component, you are ready to install it on the Component Palette. The class files for your new component must be on your classpath.
To install a completed component,
- Choose Tools|Configure Palette or right-click the Component Palette and choose Properties.
The Palette Properties dialog box appears.
- Select the Pages tab.
- In the Pages column, select the page on the Component Palette on which you want the component to appear.
- Select the class name for the component from either the Add from Archive page or the Add from Package page.
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.
- Choose Install when you are finished.
- Choose OK to close the dialog box.
Specifying a component image
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 use the Palette Properties dialog box. The image that represents a component on the Component Palette must
- Be 16 or 32 pixels square.
- Have a file type of .GIF.
- Reside in the same directory (package) as the component.
To specify an image for your component on the Component Palette with the Palette Properties dialog box,
- Choose Tools|Configure Palette or right-click the Component Palette and choose Properties.
- Select the Pages tab.
- Select the page your component is on and select the component.
- Choose Properties to display the Item Properties dialog box.
- Choose Select Image and specify the .GIF file you want for your component.
- Choose OK to close the Item Properties dialog box.
- Choose OK when you are finished.
Specifying a Component Palette page
If none of the existing pages on the Component Palette are appropriate for your new component, you can specify a new page for it.
To specify a new page in the Component Palette,
- In the Palette Properties dialog box, choose the Add button.
- Enter the name of the new page.
When you are through using the Palette Properties dialog box, the new page appears on the Component Palette.
Deploying beans
When you are ready to distribute your JavaBean, the Deployment Wizard can put all the necessary files for your bean into a .JAR file.
To access the Deployment Wizard, choose Wizards|Deployment Wizard.
For information on using the Deployment Wizard, click the Help button on the Deployment Wizard itself. For more complete information about deploying Java programs, including JavaBeans, see Deploying Java programs.
Creating an Enterprise JavaBean
The Enterprise JavaBean Wizard can get you started creating Enterprise JavaBeans. An Enterprise JavaBean is a non-visual bean that runs on a server. To learn more about Enterprise JavaBeans, download the Enterprise JavaBeans specification located at http://java.sun.com/products/ejb/.
To begin creating an Enterprise JavaBean,
- Choose File|New.
- Double-click the Enterprise JavaBean Wizard icon to display the Enterprise JavaBean Wizard.
- Specify the package the bean will be a part of in the Package field.
By default, the wizard uses the name of your current project.
- Specify a name for the Enterprise JavaBean in the Name of New JavaBean field.
- Specify the base class the bean will extend in the Base Class to Inherit From field.
If you want to ensure that any class you select is a JavaBean, check the Allow Only JavaBeans option. If this option is checked and you choose a class that is not a JavaBean, an error message that explains why the class is not a bean appears when you choose OK.
- Select a Session Bean or Entity Bean.
- Choose OK.
The Enterprise JavaBean Wizard creates a class for you that is a valid JavaBean. Within the class are several methods defined with empty bodies that you will need to fill in. For example, this is the code generated for a typical Session bean:
package Enterprise;
import java.rmi.*;
import javax.ejb.*;
public class NewSessionBean implements SessionBean {
SessionContext context;
public NewSessionBean() {
}
public void ejbCreate() throws RemoteException, CreateException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void ejbRemove() throws RemoteException {
}
public void setSessionContext(SessionContext context) throws RemoteException {
this.context = context;
}
}