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.

How to create a confirmation dialog in Java ME

From Wiki
Jump to: navigation, search

This example shows how to create a confirmation dialog in Java ME by using standard LCDUI Alert class. This dialog could be used for example for confirming closing of a MIDlet.

Article Metadata
Created: jarmlaht (19 Feb 2008)
Last edited: hamishwillee (13 Aug 2013)

Source code

The full source code for a test MIDlet:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ConfirmationMIDlet extends MIDlet implements CommandListener {
private Form form;
private Alert alert;
private Command exitCommand;
public void startApp() {
form = new Form("ConfirmationMIDlet");
exitCommand = new Command("Exit", Command.EXIT, 1);
public void pauseApp() {
public void destroyApp(boolean unconditional) {
public void commandAction (Command c, Displayable d) {
if (c == exitCommand) {
showConfirmation("Confirmation", "Do you really want to exit?");
private void closeAlert() {
alert = null;
protected void showConfirmation(String title, String text) {
alert = new Alert(title, text, null, AlertType.CONFIRMATION);
alert.addCommand(new Command ("Yes", Command.OK, 1));
alert.addCommand(new Command("No", Command.CANCEL, 1));
alert.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("Yes")) {
if (c.getLabel().equals("No")) {
Display.getDisplay(this).setCurrent(alert, form);
This page was last modified on 13 August 2013, at 06:16.
81 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.