Reputation: 1511
I am trying to learn J2ME and I have a problem with forms. My problem is that I have two forms, a ‘main’ form and a ‘greetings’ form. I can’t figure out how to call the ‘greetings ‘form from the’ main’ form. I want once the midlet is launched; the greetings form is called directly by the main form. A sample of my code is shown below.
This code is for the greetings Form (greetingsClass.java)
import javax.microedition.lcdui.*;
public class greetingsClass {
public Display greetingsDisplay;
public Form loginform;
public void login()
{
loginform = new Form("Login Class Form");
loginform.append("Ok! Iside login Classs form");
greetingsDisplay = Display.getDisplay(this);
greetingsDisplay.setCurrent(loginform);
}
}
This code is for the main form (FormMIDlet.java)
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class FormMIDlet extends MIDlet {
public Display FormMIDletdisplay;
public Form MainMIDletform;
public LoginClass LoginFormObject;
public void startApp() {
MainMIDletform = new Form("Main Class Form");
MainMIDletform.append("Calling LoginClass Form");
FormMIDletdisplay = Display.getDisplay(this);
FormMIDletdisplay.setCurrent(MainMIDletform);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}
I want the form in the first part of the code (greetingsClass.java)
to be shown in the second part which is the mainform of the midlet (FormMIDlet.java).
Upvotes: 0
Views: 2876
Reputation: 14022
I hope this code help you:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
public class myMidlet extends MIDlet implements CommandListener {
private Form greetings;
private Form MainForm;
private Command okCommand;
Display display;
public myMidlet() {
display = Display.getDisplay(this);
greetings = new Form("greetings");
MainForm = new Form("MainForm");
okCommand = new Command("Ok", Command.OK, 0);
MainForm.addCommand(okCommand);
MainForm.setCommandListener(this);
}
public void startApp() {
MainForm.append("This is MainForm");
display.setCurrent(MainForm);
}
public void pauseApp() {
...
}
public void destroyApp(boolean unconditional) {
...
}
public void commandAction(Command cmnd, Displayable dsplbl) {
if (cmnd == okCommand) {
greetings.append("This is greeting Form");
display.setCurrent(greetings);
}
}
}
The Display class is the display manager that is instantiated for each active MIDlet and provides methods to retrieve information about the device's display capabilities. A screen is made visible by calling the Display.setCurrent() method.
A Form is a screen that contains an arbitrary mixture of items (images, text, text fields, choice groups, for instance.)Form is a child of screen and Screen implements the Displayable interface.
A Displayable class is a UI element that can be shown on the device's screen while the Display class abstracts the display functions of an actual device's screen and makes them available to you. It provides methods to gain information about the screen and to show or change the current UI element that you want displayed. Thus, a MIDlet shows a Displayable UI element on a Display using the setCurrent(Displayable element) method of the Display class.A Displayable class implements Displayable interface.
Displayable object is an object that has the capability of being placed on the display.
References:
MIDP GUI Programming
J2ME Tutorial
Upvotes: 3