You Are Here:

Community: Wiki

This page was last modified on 17 September 2009, at 20:30.

How to create a confirmation dialog in Java ME

From Forum Nokia Wiki

Reviewer Approved   
Reviewer Approved   

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.


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);
form.setCommandListener(this);
form.addCommand(exitCommand);
Display.getDisplay(this).setCurrent(form);
}
 
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() {
Display.getDisplay(this).setCurrent(form);
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")) {
notifyDestroyed();
}
if (c.getLabel().equals("No")) {
closeAlert();
}
}
});
Display.getDisplay(this).setCurrent(alert, form);
}
}

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia