Yes/No dialog in Java ME

By : darius
Source: Stackoverflow.com
Question!

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
}
By : darius


Answers

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.



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 ...

By : Telcontar


This video can help you solving your question :)
By: admin