Hi there
i m new to GUI programming and i have started it from midlet. the problem is, i have made separate classes for each page/form that is to be displayed on a mobile screen but dont know how to link them.
Like i want when my login successful, my mobile screen should move to list menu, and when i select a list menu, it should move to another screen, i have made separate classes for each intended screen.
Both the code of the classes i have pasted.
Anyone please help soon.
Thanks in advance.

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class LoginExample extends MIDlet implements CommandListener{
  private Display display;
  private TextField userName,password;
  public Form form;
  private Command login,cancel;
  private Image img, imge, img2;

  public LoginExample() {
    form = new Form("Sign in");
    userName = new TextField("LoginID:", "", 30, TextField.ANY);
    password = new TextField("Password:", "", 30, TextField.PASSWORD);
    cancel = new Command("Cancel", Command.CANCEL, 2);
    login = new Command("Login", Command.OK, 2);
    try{
      img = Image.createImage("/logo.png");
      imge = Image.createImage("/front_left1_bad.png");
      img2 = Image.createImage("/Congratulations-1.png");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }
  }

   public void startApp() {
    display = Display.getDisplay(this);
    try{form.append(img);}catch(Exception e){}
    form.append(userName);
    form.append(password);
    form.addCommand(cancel);
    form.addCommand(login);
    form.setCommandListener(this);
    display.setCurrent(form);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }

  public void validateUser(String name, String password) {
    if (name.equals("abdullah") && password.equals("abdullah")) {
      showMsg();
    } else {
      tryAgain();
    }
  }

  public void showMsg() {
    Alert success = new Alert("Login Successfully",
    "Your Login Process is completed!",
     img2, AlertType.INFO);
    success.setImage(img2);
    userName.setString("");
    password.setString("");
    display.setCurrent(success, form);

  }

  public void tryAgain() {
    Alert error = new Alert("Login Incorrect", "Please     try again", imge, AlertType.ERROR);
    error.setTimeout(900);
    error.setImage(imge);
    userName.setString("");
    password.setString("");
    display.setCurrent(error, form);
  }

  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if(label.equals("Cancel")) {
      destroyApp(true);
    } else if(label.equals("Login")) {
      validateUser(userName.getString(), password.getString());
          ExclusiveChoiceList obj3 = new ExclusiveChoiceList();
    obj3.startApp();
    }
  }
  
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ExclusiveChoiceList extends MIDlet{
  private Display display;
  private List list;

  public ExclusiveChoiceList() {
    list = new List("Port Management System", Choice.EXCLUSIVE);
  }

  public void startApp(){
    display = Display.getDisplay(this);
    list.append("Vessels", null);
    list.append("Cargo", null);
    list.append("Fleets", null);
//    list.append("Chak De..", null);
//    list.append("Hum Aapke Hain Kaun", null);
    display.setCurrent(list);
  }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }
}

Dani AI

Generated

Short diagnosis: as said, only one class in an MIDP app should extend MIDlet. Screens themselves should be Displayable objects (Form, List, Canvas), not separate MIDlets. Create your UI screens as Displayable subclasses and switch between them from the single MIDlet using Display.setCurrent(...). Do not instantiate another MIDlet or call its startApp().

A simple pattern to follow: make your menu a List subclass (or a factory that returns a List) and let the MIDlet show it. The screen can receive the MIDlet (or a small callback interface) so it can ask for navigation without trying to manage the MIDlet lifecycle.

Example skeleton (adapt this to your names):

public class ExclusiveChoiceList extends List implements CommandListener {
  private Command open = new Command("Open", Command.OK, 1);
  private MIDlet parent;

  public ExclusiveChoiceList(MIDlet parent) {
    super("Port Manager", Choice.EXCLUSIVE);
    this.parent = parent;
    append("Vessels", null);
    append("Cargo", null);
    addCommand(open);
    setCommandListener(this);
  }

  public void commandAction(Command c, Displayable d) {
    if (c == open) {
      int idx = getSelectedIndex();
      // create next Displayable and show it:
      Display.getDisplay(parent).setCurrent(/* next Displayable */);
    }
  }
}

From your login MIDlet, after successful validation just create and show the menu:

ExclusiveChoiceList menu = new ExclusiveChoiceList(this);
Display.getDisplay(this).setCurrent(menu);

Practical tips: never call notifyDestroyed() or startApp() on other classes; keep heavy work (network, DB) off the UI thread; pass the MIDlet or a tiny navigation interface to child screens so they can request transitions; free large images and reuse them to reduce OOME risk. This pattern keeps lifecycle control in one MIDlet and makes navigation predictable.

Recommended Answers

All 2 Replies

Only one class can extend MIDlet, other classes can receive it only as parameter. What you did above is as if you create two classes with main method and try pass parameters from each other (which possible, but requires other things). Besides, only one MIDlet can be running at any time, any other is either put to sleep or stopped...

sorry, way too late. It was urgent, so your reply was not timely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.