Programming Tutorials

GUI components and menu based J2ME Applications.

By: Fazal in J2ME Tutorials on 2022-09-16  

J2ME is a Java platform for developing mobile applications. It includes various libraries and APIs for creating mobile applications. In J2ME, we can create GUI components and menu-based applications by using the javax.microedition.lcdui package.

Here is an example of a J2ME application that includes GUI components and menus:

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

public class J2meApp extends MIDlet implements CommandListener {
    private Display display;
    private Form form;
    private Command exitCommand;
    private Command menuCommand;
    private Command backCommand;
    private List menuList;

    public J2meApp() {
        display = Display.getDisplay(this);

        form = new Form("J2ME Application");
        exitCommand = new Command("Exit", Command.EXIT, 0);
        menuCommand = new Command("Menu", Command.SCREEN, 1);
        backCommand = new Command("Back", Command.BACK, 0);
        form.addCommand(exitCommand);
        form.addCommand(menuCommand);
        form.setCommandListener(this);

        menuList = new List("Menu", List.IMPLICIT);
        menuList.append("Option 1", null);
        menuList.append("Option 2", null);
        menuList.addCommand(backCommand);
        menuList.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            notifyDestroyed();
        } else if (c == menuCommand) {
            display.setCurrent(menuList);
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == List.SELECT_COMMAND) {
            int index = menuList.getSelectedIndex();
            Alert alert = new Alert("Selected Option", "You selected option " + (index + 1), null, AlertType.INFO);
            alert.setTimeout(2000);
            display.setCurrent(alert, menuList);
        }
    }
}

In this example, we create a J2ME application that contains a form with two commands: "Exit" and "Menu". When the user clicks on the "Menu" command, a list of options is displayed. The user can select an option, and an alert message is displayed with the selected option.

The J2ME application is built using the MIDlet class, which provides the basic framework for creating mobile applications. We create the GUI components using the javax.microedition.lcdui package, which provides the necessary classes for building mobile user interfaces.

We also implement the CommandListener interface, which provides the necessary methods for handling user actions, such as button clicks and menu selections.

Overall, this J2ME application demonstrates how to create GUI components and menu-based applications using Java.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)