This is the first time I have used a confirm box and I'd like some advice on how to use it please, I want to use the users input of "Yes or No" but not sure how to do it? If I wanted to reference the input from the JOptionPane in an if statement how would go about it?
JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);
Thank you.
use it like:
int result = JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);
if (JOptionPane.YES_OPTION == result) {
System.out.println("yes");
} else if (JOptionPane.NO_OPTION == result) {
System.out.println("No");
}else{
System.out.println("Nothing");
}
Also find the option types and return values below(from source):
/**
* Type meaning Look and Feel should not supply any options -- only
* use the options from the <code>JOptionPane</code>.
*/
public static final int DEFAULT_OPTION = -1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_OPTION = 0;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_CANCEL_OPTION = 1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int OK_CANCEL_OPTION = 2;
//
// Return values.
//
/** Return value from class method if YES is chosen. */
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
/** Return value form class method if OK is chosen. */
public static final int OK_OPTION = 0;
/** Return value from class method if user closes window without selecting
* anything, more than likely this should be treated as either a
* <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
public static final int CLOSED_OPTION = -1;
Also, don't do int check directly for response value, like if(1==result)
for NO_OPTION
, always use constants from the JoptionPane
class.