Previous Next

Bean Works Tutorial

Step 1: Creating a simple component

Using the Bean Wizard, you can generate almost all the code you need for Hello1, a simple "Hello World" component. The only code you have to add is the code that defines and draws the "Hello World" string.

The framework provides a default user interface for the application:

The default menus provided by the framework are:

The default toolbar provides icons for Open, Undo, and Redo.

Menu and toolbar items are enabled or disabled based on what is supported by the component. For example, your data editing features must be based on the framework's command processing mechanism in order for undo and redo to be supported. See Step 3 for an example.

Generating Hello1 code with the wizard

Hello1 source code

Hello1View.java
Hello1.java
Hello1Resources.java
Hello1BeanInfo.java

Compiling and running Hello1


Generating Hello1 code

To generate the code for Hello1, launch the Bean Wizard from the Bean Works program group. Make sure Create a New Project is selected and advance to the project definition screen.

This screen creates default values for the project based on the name you enter, including names for the view and model. It also creates a default project subdirectory, based on the name, under the specified project parent directory.

Because the Hello1 component displays a static string that cannot be modified and saved by the user, it doesn't need to have a model. To prevent the wizard from generating a model class, make sure the Model box is not checked:

Wizard Project Definition Screen

Press the Next>> button to go to the Project Options screen. This screen lets you specify whether you want to provide clipboard support (through a selection class) and whether you want to add customized menus to the interface for your component. Make sure neither box is selected and press the Next>> button.

The next screen provides a set of tabbed panels that let you extend and customize the component. This simple component uses the defaults, so press Next>> again to get to the code generation screen.

The code generation screen lists the files that will be generated for the component. The primary files for Hello1 are the view (Hello1View), the controller (Hello1), and the resource file (Hello1Resources).

The wizard also generates the WizGen.java file and batch files you'll use later to build and run the component. The WizGen.java file is an interface that is implemented by all the classes generated by the wizard for the project, and provides information that is used by the wizard if you reopen the project. Do not remove the WizGen.java file from your project directory!

Press Generate All Files to generate the code. Use any code editor to add more code to the files generated by the wizard.


Hello1 source code

The source code shown here differentiates between code generated by the wizard, and code you add or modify. Wizard-generated code is shown in green.

See WebRunner\BeanTools\samples\hello1 for full source code files for each class. The source code shown here does not show the comments generated by the wizard. Do NOT remove these comments from the actual source code files, or you won't be able to reopen the project with the wizard.


Hello1View.java

Hello1View provides the customized functionality of the Hello1 component, drawing the string "Hello World" into the window. You need to add only one line of code, drawing the string at a specified location from within the paint method. This method is called whenever the view needs to be drawn (or redrawn) to the screen.

The other Hello1View method, getPreferredSize, is called to set the size of the view. This method is generated automatically. You don't need to modify it unless you want to specify a different default size for the view.

Extend the default view class
public class Hello1View extends ModelView implements WizGen {
public Hello1View() {
}
Draw the "Hello World" string
public void paint(Graphics g) {
    g.drawString("Hello World", 10, 50);
}
public Dimension getPreferredSize() {
    return new Dimension(400, 300);
}

Top of page

Hello1.java

Hello1 is the controller for the component, and defines the main method that is called when the component is launched. Hello1 also builds the user interface for the component.

Extend the default controller class
public class Hello1 extends ComponentController implements WizGen {
Get custom resources from the Hello1Resources class, and create the view.
public Hello1() {
try	{
	   setResourceBundle(ResourceBundle.getBundle("hello1.Hello1Resources"));
	}
	catch (MissingResourceException e) {
	   new ExceptionDialog(null, "Can't find resource file", e);
	}
    setView(new Hello1View());
}
main method that launches the component
public static void main(String args[]) {
    setApplicationMain(true);
    new Hello1();
}
}

Top of page

Hello1Resources.java

Hello1Resources contains string resources used to construct the component, including everything from the component name to the strings used in user interface elements such as menus and flyover help. These strings can be localized to create versions of your program for international languages.

The Hello1Resources class provides only one special string resource, the component name.

Extend the java.util class ListResourceBundle
public class Hello1Resources extends ListResourceBundle {
public Object[][] getContents() {
    return contents;
}
Special resources for Hello1
static final Object[][] contents =
{
     {"Application", "Hello1"},
};
};

Top of page

Hello1BeanInfo.java

This file is generated for the user to change the property, method, and event descriptors that are displayed in a visual builder. By default, everything is displayed to the developer inside a visual builder or a tester like Bean Tester.


Top of page

Compiling and running Hello1

You can compile and run the files either from the wizard or from the command line:

Note: If you have trouble successfully executing the build or run batch files, you may not have set your JAVA_HOME, CLASSPATH, or PATH variables correctly. See the product README file for information on configuring these variables.


Previous Next

Copyright © Taligent, Inc. 1996 - 1997.
Copyright
© IBM Corporation 1996 - 1997.
All Rights Reserved.