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

<pre><code>if (YesNoDialog.ask("Are you sure?") == true) {
  // yes was chosen
} else {
  // no was chosen
}
</code></pre>

[{'answer_id': 56970, 'author': 'Telcontar', 'author_id': 518, 'author_profile': 'https://Stackoverflow.com/users/518', 'pm_score': -1, 'selected': False, 'text': '<p>I dont have programed in Java ME, but i found in it\'s reference for optional packages the\n<a href="http://java.sun.com/javame/reference/apis/jsr209/" rel="nofollow noreferrer">Advanced Graphics and User Interface API</a>, and it\'s used like the Java SE API to create these dialogs with the <a href="http://java.sun.com/javame/reference/apis/jsr209/javax/swing/JOptionPane.html" rel="nofollow noreferrer">JOptionPane Class</a></p>\n\n<blockquote>\n  <p><code>int JOptionPane.showConfirmDialog(java.awt.Component parentComponent, java.lang.Object &gt;message, java.lang.String title, int optionType)</code></p>\n</blockquote>\n\n<p>Return could be\n<code>JOptionPane.YES_OPTION</code>, <code>JOptionPane.NO_OPTION</code>, <code>JOptionPane.CANCEL_OPTION</code>...</p>\n'}, {'answer_id': 63063, 'author': 'Carlos Carrasco', 'author_id': 7027, 'author_profile': 'https://Stackoverflow.com/users/7027', 'pm_score': 4, 'selected': True, 'text': '<p>You need an <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Alert.html" rel="nofollow noreferrer">Alert</a>:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>With 2 <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Command.html" rel="nofollow noreferrer">commands</a> ("Yes"/"No" in your case):</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>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 <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/CommandListener.html" rel="nofollow noreferrer">CommandListener</a>, and prepare your code for asynchronous execution.</p>\n\n<p>Here is an (untested!) example class based on Alert:</p>\n\n<pre><code>public class MyPrompter implements CommandListener {\n\n    private Alert yesNoAlert;\n\n    private Command softKey1;\n    private Command softKey2;\n\n    private boolean status;\n\n    public MyPrompter() {\n        yesNoAlert = new Alert("Attention");\n        yesNoAlert.setString("Are you sure?");\n        softKey1 = new Command("No", Command.BACK, 1);\n        softKey2 = new Command("Yes", Command.OK, 1);\n        yesNoAlert.addCommand(softKey1);\n        yesNoAlert.addCommand(softKey2);\n        yesNoAlert.setCommandListener(this);\n        status = false;\n    }\n\n    public Displayable getDisplayable() {\n        return yesNoAlert;\n    }\n\n    public boolean getStatus() {\n        return status;\n    }\n\n    public void commandAction(Command c, Displayable d) {\n        status = c.getCommandType() == Command.OK;\n        // maybe do other stuff here. remember this is asynchronous\n    }\n\n};\n</code></pre>\n\n<p>To use it (again, untested and on top of my head):</p>\n\n<pre><code>MyPrompter prompt = new MyPrompter();\nDisplay.getDisplay(YOUR_MIDLET_INSTANCE).setCurrent(prompt.getDisplayable());\n</code></pre>\n\n<p>This code will make the prompt the current displayed form in your app, but it <em>won\'t block your thread</em> like in the example you posted. You need to continue running and wait for a commandAction invocation.</p>\n'}]
['https://Stackoverflow.com/questions/56943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5896/']