Implementing the MIDlet main class

The MIDlet main class implements the MIDlet lifecycle requirements, creates a Gauge, and opens a Canvas implementation called HelloCanvas.

The MIDlet main class creates the following UI:

Figure: SimpleTest opening view (Series 40)

To implement the MIDlet main class:

  1. Create the SimpleTest.java class. (In the IDE, create this class as a new MIDlet and delete any prefilled code.)

  2. Import the required classes.

    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    
  3. Set SimpleTest to extend MIDlet and implement CommandListener. MIDlets use the CommandListener interface to receive high-level UI events from the platform.

    public class SimpleTest extends MIDlet implements CommandListener {
    
  4. Define the commands, interfaces, and classes used by the MIDlet.

        private HelloCanvas myCanvas;
        private Form myForm;
    

    The HelloCanvas class is an implementation of the Canvas class. HelloCanvas is discussed in detail in the next section. The Form class is a Screen that can contain a mixture of UI components: images, read-only text fields, editable text fields, editable date fields, gauges, choice groups, and custom items. In general, any subclass of the Item class can be contained within a Form. The platform handles the layout, traversal, and scrolling for the Form.

        private Gauge myGauge;
        private TextField textField;
    

    The Gauge class implements a graphical representation of an integer value, such as the bar graph in this example. It contains a current value that lies between zero and a maximum value. The MIDlet can control the current value and the maximum value. The range of values specified by the MIDlet can be larger than the number of distinct visual states possible on the device, which means that more than one value can have the same visual representation. The TextField class is an editable text component that can be placed within a Form. It can be given a piece of text that is used as the initial value.

        private Command backCommand = new Command("Back", Command.BACK, 1);
        private Command messageCommand = new Command("Message", Command.SCREEN, 1);
        private Command displayCommand = new Command("Display Message", Command.SCREEN, 1);
        private Command exitCommand = new Command("Exit", Command.EXIT, 1);
        private Command showCommand = new Command("Show Levels", Command.SCREEN, 1);
    

    The Command class is a construct that encapsulates the semantic information of an action. The behavior a command activates is not encapsulated in the Command object, that is, the Command object does not contain information about the actual action that happens when the command is activated. The action is defined in a CommandListener associated with the current Displayable. Command objects are presented in the UI, and the way they are presented can depend on the semantic information contained within them.

  5. Create a new SimpleTest object and within it a new HelloCanvas object. Use the addCommand method to add the backCommand and messageCommand to the HelloCanvas object.

        public SimpleTest() {
            myCanvas = new HelloCanvas();
            myCanvas.addCommand(backCommand);
            myCanvas.addCommand(messageCommand);
    
  6. Create a new Form object and initialize it with the text "Gauge level". Also create a new Gauge object and a new TextField object and initialize them with the values shown in the following code snippet. Use the Form.append method to add the Gauge and TextField objects to the Form object.

            myForm = new Form("Gauge level");
            myGauge = new Gauge("Value", true, 120, 10);
            textField = new TextField("Enter number", "", 3, TextField.NUMERIC);
            myForm.append(myGauge);
            myForm.append(textField);
    
  7. Use the addCommand method to add the showCommand, displayCommand, and exitCommand to the Form object.

            myForm.addCommand(showCommand);
            myForm.addCommand(displayCommand);
            myForm.addCommand(exitCommand);
    
  8. Set a CommandListener for the HelloCanvas and Form objects.

            myCanvas.setCommandListener(this);
            myForm.setCommandListener(this);
        }
    
  9. Add the mandatory MIDlet lifecycle methods.

        public void startApp() {
            Display.getDisplay(this).setCurrent(myForm);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean a) throws MIDletStateChangeException {
        }
    
  10. Implement the CommandListener.commandAction method to provide functionality for the Commands:

    • If the exitCommand is called by the user selecting Exit in the MIDlet UI, call the notifyDestroyed method.

    • If the messageCommand is called, use the HelloCanvas.newMessage method to toggle the visibility of the message text on the HelloCanvas.

    • If the backCommand is called, use the Display.setCurrent method to return to the Form in the MIDlet UI. Use the Display.getDisplay method to retrieve the correct Display for the MIDlet.

    • If the displayCommand is called, use the Display.setCurrent method to open the HelloCanvas. This redraws the screen with the contents of the HelloCanvas. Use the Display.getDisplay method to retrieve the correct Display for the MIDlet.

    • If the showCommand is called, use the TextField.getString method to copy the text contents of the textField object to valueString. Use that value with the Gauge.setValue method to manipulate the Gauge.

        public void commandAction(Command c, Displayable d) {
            if (c == exitCommand) {
                notifyDestroyed();
            }
            if (c == messageCommand) {
                myCanvas.newMessage();
            }
            if (c == backCommand) {
                Display.getDisplay(this).setCurrent(myForm);
            }
            if (c == displayCommand) {
                Display.getDisplay(this).setCurrent(myCanvas);
            }
            if (c == showCommand) {
                String valueString = textField.getString();
                int value = 0;
                if (!valueString.equals("")) {
                    value = Integer.parseInt(valueString);
                }
                myGauge.setValue(value);
            }
        }
    }

Now that you have implemented the MIDlet main class, implement the HelloCanvas class.