Example usage for javax.swing JOptionPane CANCEL_OPTION

List of usage examples for javax.swing JOptionPane CANCEL_OPTION

Introduction

In this page you can find the example usage for javax.swing JOptionPane CANCEL_OPTION.

Prototype

int CANCEL_OPTION

To view the source code for javax.swing JOptionPane CANCEL_OPTION.

Click Source Link

Document

Return value from class method if CANCEL is chosen.

Usage

From source file:ConfirmPopUps.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    int result = JOptionPane.showConfirmDialog(frame, "Continue printing?");
    // JOptionPane.showInternalConfirmDialog(desktop, "Continue printing?");

    System.out.println(JOptionPane.CANCEL_OPTION == result);
}

From source file:Main.java

public static void main(String[] args) {
    int response = JOptionPane.showConfirmDialog(null, "Save the file?", "Confirm  Save  Changes",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

    switch (response) {
    case JOptionPane.YES_OPTION:
        System.out.println("yes");
        break;/* ww  w . j a  v a 2s.  com*/
    case JOptionPane.NO_OPTION:
        System.out.println("no");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("cancel");
        break;
    case JOptionPane.CLOSED_OPTION:
        System.out.println("closed.");
        break;
    default:
        System.out.println("something else");
    }

}

From source file:GettingJOptionPaneSelectionDemo.java

public static void main(String[] a) {

    String multiLineMsg[] = { "Hello,", "World" };
    JOptionPane pane = new JOptionPane();
    pane.setMessage(multiLineMsg);/*from   w  w  w .j  a  v  a  2  s  .c o m*/
    JDialog d = pane.createDialog(null, "title");
    d.setVisible(true);
    int selection = getSelection(pane);

    switch (selection) {
    case JOptionPane.OK_OPTION:
        System.out.println("OK_OPTION");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("CANCEL");
        break;
    default:
        System.out.println("Others");
    }

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);/*from w  w w  .  j  a va  2 s.c  o m*/
    frame.setVisible(true);

    JOptionPane.showMessageDialog(frame, "A");
    JOptionPane.showMessageDialog(frame, "B", "message", JOptionPane.WARNING_MESSAGE);

    int result = JOptionPane.showConfirmDialog(null, "Remove now?");
    switch (result) {
    case JOptionPane.YES_OPTION:
        System.out.println("Yes");
        break;
    case JOptionPane.NO_OPTION:
        System.out.println("No");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("Cancel");
        break;
    case JOptionPane.CLOSED_OPTION:
        System.out.println("Closed");
        break;
    }

    String name = JOptionPane.showInputDialog(null, "Please enter your name.");
    System.out.println(name);

    JTextField userField = new JTextField();
    JPasswordField passField = new JPasswordField();
    String message = "Please enter your user name and password.";
    result = JOptionPane.showOptionDialog(frame, new Object[] { message, userField, passField }, "Login",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (result == JOptionPane.OK_OPTION)
        System.out.println(userField.getText() + " " + new String(passField.getPassword()));

}

From source file:MainClass.java

public static void main(final String args[]) {
    UIManager.put("OptionPane.cancelButtonText", "Annuler");
    UIManager.put("OptionPane.noButtonText", "Non");
    UIManager.put("OptionPane.okButtonText", "D'accord");
    UIManager.put("OptionPane.yesButtonText", "Oui");

    int result = JOptionPane.showConfirmDialog(new JFrame(), "Est-ce que vous avez 18 ans ou plus?",
            "Choisisez une option", JOptionPane.YES_NO_CANCEL_OPTION);
    if (result == JOptionPane.YES_OPTION) {
        System.out.println("Yes");
    } else if (result == JOptionPane.NO_OPTION) {
        System.out.println("No");
    } else if (result == JOptionPane.CANCEL_OPTION) {
        System.out.println("Cancel");
    } else if (result == JOptionPane.CLOSED_OPTION) {
        System.out.println("Closed");
    }//from   w ww . ja v  a  2s  .c  o m
}

From source file:Main.java

public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) {
    for (;;) {/*w w  w. j  a  v a 2 s .c o  m*/
        int option = chooser.showOpenDialog(parentComponent);
        if (option != JFileChooser.APPROVE_OPTION) {
            return JOptionPane.CANCEL_OPTION;
        }

        File file = chooser.getSelectedFile();
        if (file.exists()) {
            int op2 = showReplaceExistingFileConfirmDialog(parentComponent, file);
            if (op2 == JOptionPane.YES_OPTION) {
                return JOptionPane.YES_OPTION;
            } else if (op2 == JOptionPane.CANCEL_OPTION) {
                return JOptionPane.CANCEL_OPTION;
            }
        } else {
            return JOptionPane.YES_OPTION;
        }
    }
}

From source file:Main.java

/**
 * Displays a {@link JOptionPane} as a Confirm Dialog with OK and CANCEL buttons.
 * //from   w w w .j  av  a2  s .c om
 * @param title The title of the Dialog.
 * @param message The message of the Dialog.
 * @return "ok" or "cancel" if clicked on the respective buttons.
 */
public static String showConfirmationDialogWithOkCancelButtons(String title, String message) {
    int result = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);
    if (result == JOptionPane.OK_OPTION)
        return "ok";
    if (result == JOptionPane.CANCEL_OPTION)
        return "cancel";
    return null;
}

From source file:Main.java

public static int select(String[] selList, String msg) {
    JComboBox<String> box = new JComboBox<>(selList);
    Object msgs[] = new Object[2];
    msgs[0] = msg;/*from www.java 2  s.  com*/
    msgs[1] = box;
    int result = JOptionPane.showOptionDialog(null, msgs, msg, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (result == JOptionPane.CANCEL_OPTION)
        return -1;
    return box.getSelectedIndex();
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new JLabel("Placeholder label"));
    pack();//from w  w w.j  av a  2s.co  m
    setSize(200, 200);
    setVisible(true);

    int replaced = JOptionPane.showConfirmDialog(this, "Replace existing selection?");

    String result = "?";
    switch (replaced) {
    case JOptionPane.CANCEL_OPTION:
        result = "Canceled";
        break;
    case JOptionPane.CLOSED_OPTION:
        result = "Closed";
        break;
    case JOptionPane.NO_OPTION:
        result = "No";
        break;
    case JOptionPane.YES_OPTION:
        result = "Yes";
        break;
    default:
        ;
    }
    System.out.println("Replace? " + result);
}

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new JLabel("Placeholder label"));
    pack();//from  ww  w . j  a  va2 s. com
    setSize(200, 200);
    setVisible(true);

    int replaced = JOptionPane.showConfirmDialog(this, "Replace existing selection?");

    String result = "?";
    switch (replaced) {
    case JOptionPane.CANCEL_OPTION:
        result = "Canceled";
        break;
    case JOptionPane.CLOSED_OPTION:
        result = "Closed";
        break;
    case JOptionPane.NO_OPTION:
        result = "No";
        break;
    case JOptionPane.YES_OPTION:
        result = "Yes";
        break;
    default:
        ;
    }
    System.out.println("Replace? " + result);
}