Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:26, 8 June 2009 by tanjaluodes (Talk | contribs)

How to use two forms

From Wiki
Jump to: navigation, search

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

In SumMIDlet there is two fields where you have to fill in with two integers, selecting the command "calculate" another form will show up with the sum of these two integers you just enter in and selecting the command "exit" the MIDlet will be destroyed.

In MyResultForm there is only one field with the sum result 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;
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");
this.okCommand = new Command("Calculate", Command.OK, 0);
this.exitCommand = new Command("Exit", Command.EXIT, 1);
 
this.a = new TextField("A", "", 3, TextField.NUMERIC);
this.b = new TextField("B", "", 4, TextField.NUMERIC);
 
this.resultForm = new MyResultForm("", this.mainForm);
 
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;
private Displayable previous;
public MyResultForm(String title, Displayable previousDisplayable) {
super(title);
this.previous = previousDisplayable;
 
this.resultado = new StringItem("Result: ", "");
this.backCommand = new Command("Back", Command.BACK, 1);
 
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);
}
}

Images

First form (SumMIDlet)

Imagem1.png

Image from MyResultForm

Imagem2.png

65 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×