views:

1886

answers:

3

I'm looking for a simple solution for a yes/no dialog to use in a Java ME midlet. I'd like to use it like this but other ways are okey.

if (YesNoDialog.ask("Are you sure?") == true) {
  // yes was chosen
} else {
  // no was chosen
}
A: 

I dont have programed in Java ME, but i found in it's reference for optional packages the Advanced Graphics and User Interface API, and it's used like the Java SE API to create these dialogs with the JOptionPane Class

int JOptionPane.showConfirmDialog(java.awt.Component parentComponent, java.lang.Object >message, java.lang.String title, int optionType)

Return could be JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION ...

Telcontar
Unfortunatly this is an optional package and it's not widely supported
Stefano Driussi
A: 

Using the JOptionPane Class in Advanced Graphics and User Interface API (JSR-209) is a good suggestion. Unfortunately mobile phones do not implement that particular JSR yet so it can't be used as a general way of showing yes/no dialogues. Alas.

darius
+4  A: 

You need an Alert:

An alert is a screen that shows data to the user and waits for a certain period of time before proceeding to the next Displayable. An alert can contain a text string and an image. The intended use of Alert is to inform the user about errors and other exceptional conditions.

With 2 commands ("Yes"/"No" in your case):

If there are two or more Commands present on the Alert, it is automatically turned into a modal Alert, and the timeout value is always FOREVER. The Alert remains on the display until a Command is invoked.

These are built-in classes supported in MIDP 1.0 and higher. Also your code snippet will never work. Such an API would need to block the calling thread awaiting for the user to select and answer. This goes exactly in the opposite direction of the UI interaction model of MIDP, which is based in callbacks and delegation. You need to provide your own class, implementing CommandListener, and prepare your code for asynchronous execution.

Here is an (untested!) example class based on Alert:

public class MyPrompter implements CommandListener {

 private Alert yesNoAlert;

 private Command softKey1;
 private Command softKey2;

 private boolean status;

 public MyPrompter() {
  yesNoAlert = new Alert("Attention");
  yesNoAlert.setString("Are you sure?");
  softKey1 = new Command("No", Command.BACK, 1);
  softKey2 = new Command("Yes", Command.OK, 1);
  yesNoAlert.addCommand(softKey1);
  yesNoAlert.addCommand(softKey2);
  yesNoAlert.setCommandListener(this);
  status = false;
 }

 public Displayable getDisplayable() {
  return yesNoAlert;
 }

 public boolean getStatus() {
  return status;
 }

 public void commandAction(Command c, Displayable d) {
  status = c.getCommandType() == Command.OK;
  // maybe do other stuff here. remember this is asynchronous
 }

};

To use it (again, untested and on top of my head):

MyPrompter prompt = new MyPrompter();
Display.getDisplay(YOUR_MIDLET_INSTANCE).setCurrent(prompt.getDisplayable());

This code will make the prompt the current displayed form in your app, but it won't block your thread like in the example you posted. You need to continue running and wait for a commandAction invocation.

Carlos Carrasco
Alert doesn't support addCommand in MIDP 1.0 http://java.sun.com/javame/reference/apis/jsr037/javax/microedition/lcdui/Alert.html
Casebash