Namespaces
Variants
Actions

How to use two forms

Jump to: navigation, search
SignpostIcon Asha UI.png
Article Metadata

Article
Created: _katy_ (25 Aug 2008)
Last edited: hamishwillee (11 Jan 2012)


This example shows how to use two forms. One form is inside the MIDlet class (SumMIDlet) and the other one is called MyResultForm.

In SumMIDlet there are two fields where you may fill in with two integers. After selecting the command "calculate", another form will be visible with the sum of these two integers. When you just enter in and select the command "exit", the MIDlet will be destroyed.

In MyResultForm there is only one field with the result of the sum and the command "back" to return to the first form (SumMIDlet).

Code

public class SumMIDlet extends MIDlet implements CommandListener {
 
public static Display d;
private Form mainForm; //the SumMIDlet's form
private Command okCommand;
private Command exitCommand;
private TextField a;
private TextField b;
 
private MyResultForm resultForm;
 
public SumMIDlet() {
d = Display.getDisplay(this);
this.mainForm = new Form("Equation"); //Creates a Form with a title
this.okCommand = new Command("Calculate", Command.OK, 0); //creates an "Ok" command
this.exitCommand = new Command("Exit", Command.EXIT, 1); //creates an "Exit" command
 
this.a = new TextField("A", "", 3, TextField.NUMERIC); //creates a field which will contain the first value
this.b = new TextField("B", "", 4, TextField.NUMERIC);//creates a field which will contain the second value
 
this.resultForm = new MyResultForm("", this.mainForm); //creates a "MyResultForm" object
 
this.mainForm.append(this.a);
this.mainForm.append(this.b);
this.mainForm.addCommand(this.exitCommand);
this.mainForm.addCommand(this.okCommand);
this.mainForm.setCommandListener(this);
}
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
 
protected void startApp() throws MIDletStateChangeException {
this.d.setCurrent(this.mainForm);
}
 
public void commandAction(Command cmd, Displayable arg1) {
 
if(cmd == this.okCommand) {
int a = Integer.parseInt(this.a.getString());
int b = Integer.parseInt(this.b.getString());
int resultado = a+b;
this.resultForm.setResultado(String.valueOf(resultado));
this.d.setCurrent(this.resultForm);
} else if(cmd == this.exitCommand) {
try {
this.destroyApp(true);
this.notifyDestroyed();
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
 
}
 
}
public class MyResultForm extends Form implements CommandListener {
 
private StringItem resultado;
private Command backCommand; //command to return to the first form
private Displayable previous;
 
//Constructor: receives the Form's title and the previous displayable. Any kind of displayable class (Alert, TextBox, List) can be
//sent to this second input paramenter.
public MyResultForm(String title, Displayable previousDisplayable) {
super(title);
this.previous = previousDisplayable;
 
this.resultado = new StringItem("Result: ", ""); //shows the result of the sum
this.backCommand = new Command("Back", Command.BACK, 1); //creates the back command
 
this.append(this.resultado);
this.addCommand(this.backCommand);
this.setCommandListener(this);
}
 
protected void setResultado(String resultado) {
this.resultado.setText(resultado);
}
 
public void commandAction(Command command, Displayable arg1) {
if(command == this.backCommand)
SumMIDlet.d.setCurrent(this.previous); //returns to the first form
}
}

Images

First form (SumMIDlet)

Imagem1.png

Image from MyResultForm

Imagem2.png

This page was last modified on 11 January 2012, at 03:54.
100 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved