Implementing the PasswordScreen class

This message asks the user for a password and forwards it to the main SATSAMIDlet class for message decryption.

To create the class:

  1. Assign the class to the package satsa and import the required classes.

    package satsa;
    
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.TextField;
  2. Create the class by extending Form and implementing CommandListener.

    // Shows a password field used to decrypt a message
    class PasswordScreen
      extends Form
      implements CommandListener
    {
      private final SATSAMIDlet midlet;
      private final Command commandBack =
        new Command("Back", Command.BACK, 1);
      private final Command commandDecrypt =
        new Command("Decrypt", Command.ITEM, 1);
      private int index;
      TextField passwordField;
    
      PasswordScreen(SATSAMIDlet midlet)
      {
        super(null);
        this.midlet = midlet;
        createForm();
      }
      
      public void commandAction(Command c, Displayable d)
      {
        if (c==commandBack)
        {
          midlet.showEncryptedMessage(index);
        }
        if (c==commandDecrypt)
        {
          midlet.showDecryptedMessage(index,
              passwordField.getString());
        }
      }
    
      void setIndex(int index)
      {
        this.index = index;
        createForm();
      }
    
      private void createForm()
      {
        deleteAll();
        setTitle("Enter password to decrypt.");
        passwordField = new TextField("Password",
            "",
            16,
            TextField.PASSWORD);
        append(passwordField);
        addCommand(commandBack);
        addCommand(commandDecrypt);
        setCommandListener(this);
      }
    }