get Yes No Dialog via JOptionPane - Java Swing

Java examples for Swing:JOptionPane

Description

get Yes No Dialog via JOptionPane

Demo Code


import javax.swing.JOptionPane;

public class Main{
    public static boolean getYesNo(String settingQ, Boolean flg) {
        String[] options = { "Yes", "No" };
        String defaultAnswer = flg != null && flg == true ? "Yes" : "No";
        String input = getInput(settingQ, options, defaultAnswer);
        return (input.equals("Yes") ? true : false);

    }/*from   ww  w  .  j  av a 2s.  c  o m*/
    public static String getInput(String msg, String defaultInput) {
        // msg = something like: "Please enter the MTP directory, e.g., '/sdcard/Music'"

        String input = (String) JOptionPane.showInputDialog(msg,
                defaultInput);

        return input;

    }
    public static String getInput(String msg, String[] options,
            String defaultInput) {
        // msg = something like: "Please enter the MTP directory, e.g., '/sdcard/Music'"

        String input = (String) JOptionPane.showInputDialog(null, msg,
                "Please select an option", JOptionPane.PLAIN_MESSAGE, null,
                options, defaultInput);
        return input;

    }
}

Related Tutorials