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 file. (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 variables and commands 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);
            }
        }
    
  11. When showing Canvas on a Series 40 full touch device (a device with no physical keyboard), the platform adds an additional Open keypad command to the Options menu. This is to maintain compatibility with older applications on full touch devices. When selecting Open keypad, a virtual keyboard is displayed. However, in this example there is no need for the virtual keyboard on the colored screen. Java Runtime 2.0.0 for Series 40 provides the static hideOpenKeypadCommand method for disabling theOpen keypad command. Implement a helper method for the required functionality:

    1. Check the type of the physical keyboard by querying System.getProperty("com.nokia.keyboard.type").

    2. If the returned type String equals "None", the current device is a full touch device, and the method hideOpenKeypadCommand(true) of the class VirtualKeyboard in the package com.nokia.mid.ui can be called.

    3. If the keyboard type check for some reason fails, catch possible runtime exceptions.

      private void hideOpenKeypadCommand() {
              try {
                  String keyboardType = System.getProperty("com.nokia.keyboard.type");
                  if (keyboardType.equals("None")) {
                      // Full touch device detected
                      com.nokia.mid.ui.VirtualKeyboard.hideOpenKeypadCommand(true);
                  }
              } catch (Exception e) {
                  // Do nothing
              }
          }
      }
    4. Add a method call to this method at the end of the SimpleTest constructor (after the setCommandListener lines):

       hideOpenKeypadCommand();

    Note: Because of the keyboard checks and the exception catching, the resulting JAR file can be run on full touch devices as well as on earlier devices. However, because of the VirtualKeyboard.hideOpenKeypadCommand(true) method call, the code will not compile on Nokia SDK 1.1 for Java or earlier. That is why the method code is commented out in the downloadable source package.

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