Form is a top-level component that serves as the root for the UI library. This Container handles the title and menus and allows content to be placed between them. By default, the form's central content (the content pane) is scrollable. Form contains TitleBar, MenuBar and a ContentPane. Invocations of Form's addComponent method are delegated to the content pane's addComponent. The same applies to most composite related methods (for example setLayout, getComponent, and so forth).
The following code demonstrates the creation and setup of a form:
// 1. Create a Form Form mainForm = new Form("Form Title"); // 2. Set LayoutManager mainForm.setLayout(new BorderLayout()); // 3. Add a Label to the center of Form content pane mainForm.addComponent(BorderLayout.CENTER, new Label(“Hello World”)); // 4. Set Transitions animation of Fade mainForm.setTransitionOutAnimator(CommonTransitions.createFade(400)); // 5. Add Command key mainForm.addCommand(new Command("Run", 2)); // 6. Show it mainForm.show();
The following notes correspond to the comments in the above code:
The first line of code creates a form using a constructor that lets you set the form title. The other frequently used form constructor is the no-argument constructor.
Next the code specifies the layout manager of the form. Layout managers are discussed later in this guide.
The next bit of code adds a label to the form content pane. Adding components to a Form (which is a Container) is done with addComponent(Component cmp) or addComponent(Object constraints, Component cmp), where constraints are the locations in the layout manager, BorderLayout.
A Transition is the movement effect action that occurs when switching between forms. See the Animations and transitions chapter.
Form has menus to emulate — for example — the device soft keys. To set such a menu bar item, Command, use the addCommand(Command cmd) method. Command mapping is discussed in Commands and menus section later in this guide.
The show method displays the current form on the screen.
The form created by the example code is shown in Figure 1.
Figure: Form example