How to use two forms
Article Metadata
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)
Image from MyResultForm




30 Sep
2009
Forms are the basic User Interface components of an application. Forms can be used to create a Login page, Registration page or application page in a S60 application. This article presents code snippests to create two forms in JAVA ME for S60 devices- first form contains two fields, where user can input data(integers) and second form displays the addition of those 2 integers.
The code snippests for the same are illustrated with comments. So that the Beginners can understand the concept of using multiple forms and can apply it in their own application. Screen-shots are also provided for our ease.
19 Sep
2009
There are always many forms in any application.This article has given out a solution to handle two forms on users' action. one form where user fill in details and upon users' button click or menu event user will be redirected to another form where he can get results of the supplied value and get back to first form from menu back button.
Code Snippet on handling of muliple froms,How to handle user actions is coded excellent. I have tried given code and its just runs and gives same output as shown in pictures given in this article.