Example usage for javax.swing JOptionPane OK_CANCEL_OPTION

List of usage examples for javax.swing JOptionPane OK_CANCEL_OPTION

Introduction

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

Prototype

int OK_CANCEL_OPTION

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

Click Source Link

Document

Type used for showConfirmDialog.

Usage

From source file:Main.java

public static void main(String[] args) {
    Image image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    JPanel gui = new JPanel(new BorderLayout());

    JLabel clouds = new JLabel(new ImageIcon(new BufferedImage(250, 100, BufferedImage.TYPE_INT_RGB)));
    gui.add(clouds);/*from  www  . ja  v  a  2s. co m*/

    JOptionPane.showConfirmDialog(null, gui, "Title", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, new ImageIcon(image));
}

From source file:JSliderOnJOptionPane.java

public static void main(final String[] args) {
    JFrame parent = new JFrame();

    JOptionPane optionPane = new JOptionPane();
    JSlider slider = getSlider(optionPane);
    optionPane.setMessage(new Object[] { "Select a value: ", slider });
    optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
    optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = optionPane.createDialog(parent, "My Slider");
    dialog.setVisible(true);//from w  w w  .j av a  2 s .c o  m
    System.out.println("Input: " + optionPane.getInputValue());
}

From source file:Main.java

public static void main(String[] args) {
    JTextField ipField = new JTextField(10);
    JTextField portField = new JTextField(10);
    JPanel panel = new JPanel();
    panel.add(new JLabel("IP:"));
    panel.add(ipField);/*  w ww  .  java2  s.c  o  m*/

    panel.add(Box.createHorizontalStrut(15));
    panel.add(new JLabel("Port:"));
    panel.add(portField);

    int result = JOptionPane.showConfirmDialog(null, panel, "Enter Information", JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) {
        System.out.println("IP: " + ipField.getText());
        System.out.println("Port: " + portField.getText());
    }
}

From source file:Main.java

public static void main(String[] args) {
    JTextField xField = new JTextField(5);
    JTextField yField = new JTextField(5);

    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("x:"));
    myPanel.add(xField);/*from ww w .  jav a2 s  .c o  m*/
    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
    myPanel.add(new JLabel("y:"));
    myPanel.add(yField);

    int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
            JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        System.out.println("x value: " + xField.getText());
        System.out.println("y value: " + yField.getText());
    }

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Show Input Dialog Box");
    button.addActionListener(e -> {//from   w  w w.  j  av  a 2 s. c om
        JTextField xField = new JTextField(5);
        JTextField yField = new JTextField(5);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("x:"));
        myPanel.add(xField);
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("y:"));
        myPanel.add(yField);

        int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
                JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println("x value: " + xField.getText());
            System.out.println("y value: " + yField.getText());
        }
    });
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField account = new JTextField(10);
    JPanel accountPanel = new JPanel(new GridLayout());
    accountPanel.add(account);/*from  w  ww.  j ava2 s  . com*/
    accountPanel.setBorder(new TitledBorder("Account"));

    String[] firstDigitList = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

    JLabel firstDigitListLabel = new JLabel("Leading Digit Change");
    JPanel firstDigitListPanel = new JPanel(new BorderLayout(4, 2));
    firstDigitListPanel.add(firstDigitListLabel, BorderLayout.WEST);
    JComboBox firstDigitCombo = new JComboBox(firstDigitList);
    firstDigitListPanel.add(firstDigitCombo);
    firstDigitCombo.setSelectedIndex(0);
    firstDigitListPanel.setBorder(new TitledBorder("LDC"));

    JPanel panel = new JPanel();
    panel.add(accountPanel);
    panel.add(firstDigitListPanel);

    int result = JOptionPane.showConfirmDialog(null, panel, "Please Enter Values",
            JOptionPane.OK_CANCEL_OPTION);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);/*from  ww  w.j av a2  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:Main.java

public static int showOptionsDialog(String message, String... options) {
    return JOptionPane.showOptionDialog(null, message, "Select", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
}

From source file:Main.java

public static int okcancel(String theMessage) {
    int result = JOptionPane.showConfirmDialog((Component) null, theMessage, "alert",
            JOptionPane.OK_CANCEL_OPTION);
    return result;
}

From source file:Main.java

/**
 * Displays a {@link JOptionPane} as a Confirm Dialog with OK and CANCEL buttons.
 * /*w  ww  . j  ava 2  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;
}