Example usage for javax.swing JOptionPane showOptionDialog

List of usage examples for javax.swing JOptionPane showOptionDialog

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
        int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException 

Source Link

Document

Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

Usage

From source file:Main.java

public static void main(String[] args) {
    Object[] options1 = { "Try This Number", "Choose A Random Number", "Quit" };

    JPanel panel = new JPanel();
    panel.add(new JLabel("Enter number between 0 and 1000"));
    JTextField textField = new JTextField(10);
    panel.add(textField);//from ww w.  j a  v a 2s.  com

    int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number", JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE, null, options1, null);
    if (result == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, textField.getText());
    }
}

From source file:Main.java

public static void main(String[] args) {
    JComponent parentComponent = null;
    Object message = "How  is JOptionPane?";
    String title = "JOptionPane Option  Dialog";
    int messageType = JOptionPane.INFORMATION_MESSAGE;
    Icon icon = null;//w ww . j  ava 2s . c  o  m
    Object[] options = new String[] { "A", "B", "C" };
    Object initialOption = options[2];
    int response = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION,
            JOptionPane.QUESTION_MESSAGE, icon, options, initialOption);
    switch (response) {
    case 0:
    case 1:
    case 2:
        System.out.println("we selected:" + options[response]);
        break;
    case JOptionPane.CLOSED_OPTION:
        System.out.println("we closed the   dialog box.");
        break;
    default:
        System.out.println("I don't know what  we  did.");
    }

}

From source file:NoButtonPopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("NoButton Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            int response = JOptionPane.showOptionDialog(source, "", "Empty?", JOptionPane.DEFAULT_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, new Object[] {}, null);
            System.out.println("Response: " + response);
        }/*from www.j a  v a  2 s .  com*/
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

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

public static void main(String[] args) throws HttpException, IOException, ParserConfigurationException,
        SAXException, XPathExpressionException, JSONException, InterruptedException {
    System.out.println("Google Voice Message Archiver");
    System.out.println("Copyright (C) 2013 Kevin Carter");

    System.out.println("This program comes with ABSOLUTELY NO WARRANTY");
    System.out.println("This is free software, and you are welcome to redistribute it");
    System.out.println("under certain conditions. See the LICENSE file or");
    System.out.println("http://www.gnu.org/licenses/gpl-3.0.txt for details");
    try {/*from   w w w.  ja va2 s . c  o  m*/
        HttpClient c = new HttpClient();

        String userName = getUserName();
        String password = getPassword();

        int locationChosenIndex = JOptionPane.CLOSED_OPTION;
        if (password != null) {
            locationChosenIndex = JOptionPane.showOptionDialog(null, "Message source", "",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    Worker.ListLocation.values(), Worker.ListLocation.inbox);
        }
        if (locationChosenIndex != JOptionPane.CLOSED_OPTION) {
            int modeChosenIndex = 0;
            Worker.ArchiveMode modeChosen = null;
            Worker.ListLocation location = Worker.ListLocation.values()[locationChosenIndex];
            Worker.ArchiveMode[] availableModes = location.getAllowedModes();
            if (availableModes.length == 1) {
                modeChosen = availableModes[0];
            } else {
                modeChosenIndex = JOptionPane.showOptionDialog(null, "Operation mode", "",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, availableModes,
                        Worker.ArchiveMode.archive);
                if (modeChosenIndex != JOptionPane.CLOSED_OPTION) {
                    modeChosen = availableModes[modeChosenIndex];
                }
            }

            ContactFilter filter = null;
            if (modeChosenIndex != JOptionPane.CLOSED_OPTION
                    && locationChosenIndex != JOptionPane.CLOSED_OPTION) {
                filter = buildFilter();
            }

            if (modeChosenIndex != JOptionPane.CLOSED_OPTION && locationChosenIndex != JOptionPane.CLOSED_OPTION
                    && filter != null && areYouSure(modeChosen, location, filter)) {
                assert modeChosen != null : "ZOMG";
                String authToken = getToken(userName, password);
                String rnrse = getRnrse(authToken);

                final ProgressMonitor pm = new ProgressMonitor(null, "Working", "", 0,
                        App.parseMsgsLeft(extractInboxJson(authToken, location, 1)));
                pm.setMillisToDecideToPopup(0);
                pm.setMillisToPopup(0);

                Worker worker = new Worker(authToken, rnrse, pm, modeChosen, location, filter);
                worker.addPropertyChangeListener(new ProgressPropertyChangeListener(pm));
                pm.setProgress(0);
                worker.execute();
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:Examples.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Example Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));

    JFrame frame2 = new JFrame("Desktop");
    final JDesktopPane desktop = new JDesktopPane();
    frame2.getContentPane().add(desktop);
    JButton pick = new JButton("Pick");
    pick.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Hi");
        }/*from  w ww .  j a  va 2  s  .com*/
    });
    frame2.getContentPane().add(pick, BorderLayout.SOUTH);

    JButton messagePopup = new JButton("Message");
    contentPane.add(messagePopup);
    messagePopup.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            JOptionPane.showMessageDialog(source, "Printing complete");
            JOptionPane.showInternalMessageDialog(desktop, "Printing complete");
        }
    });

    JButton confirmPopup = new JButton("Confirm");
    contentPane.add(confirmPopup);
    confirmPopup.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            JOptionPane.showConfirmDialog(source, "Continue printing?");
            JOptionPane.showInternalConfirmDialog(desktop, "Continue printing?");
        }
    });

    JButton inputPopup = new JButton("Input");
    contentPane.add(inputPopup);
    inputPopup.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            JOptionPane.showInputDialog(source, "Enter printer name:");
            // Moons of Neptune
            String smallList[] = { "Naiad", "Thalassa", "Despina", "Galatea", "Larissa", "Proteus", "Triton",
                    "Nereid" };
            JOptionPane.showInternalInputDialog(desktop, "Pick a printer", "Input",
                    JOptionPane.QUESTION_MESSAGE, null, smallList, "Triton");
            // Moons of Saturn - includes two provisional designations to
            // make 20
            String bigList[] = { "Pan", "Atlas", "Prometheus", "Pandora", "Epimetheus", "Janus", "Mimas",
                    "Enceladus", "Tethys", "Telesto", "Calypso", "Dione", "Helene", "Rhea", "Titan", "Hyperion",
                    "Iapetus", "Phoebe", "S/1995 S 2", "S/1981 S 18" };
            //        Object saturnMoon = JOptionPane.showInputDialog(source, "Pick
            // a printer", "Input", JOptionPane.QUESTION_MESSAGE, null,
            // bigList, "Titan");
            Object saturnMoon = JOptionPane.showInputDialog(source, "Pick a printer", "Input",
                    JOptionPane.QUESTION_MESSAGE, null, bigList, null);
            System.out.println("Saturn Moon: " + saturnMoon);
        }
    });

    JButton optionPopup = new JButton("Option");
    contentPane.add(optionPopup);
    optionPopup.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();

            Icon greenIcon = new DiamondIcon(Color.green);
            Icon redIcon = new DiamondIcon(Color.red);
            Object iconArray[] = { greenIcon, redIcon };
            JOptionPane.showOptionDialog(source, "Continue printing?", "Select an Option",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, iconArray, iconArray[1]);

            Icon blueIcon = new DiamondIcon(Color.blue);
            Object stringArray[] = { "Do It", "No Way" };
            JOptionPane.showInternalOptionDialog(desktop, "Continue printing?", "Select an Option",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, blueIcon, stringArray,
                    stringArray[0]);
        }
    });

    frame.setSize(300, 200);
    frame.setVisible(true);
    frame2.setSize(300, 200);
    frame2.setVisible(true);
}

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 boolean confirmationPrompt(String msg, String title, Component parent) {
    Object[] options = { "Yes", "No" };
    int n = JOptionPane.showOptionDialog(parent, msg, title, JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
    if (n == 0) {
        return true;
    } else {//from   w  w  w.  jav a 2s . c o  m
        return false;
    }
}

From source file:Main.java

/**
 * Displays a yes/no question to the user.
 * /*w w w  .  j  a  va  2s . c  o  m*/
 * @param parent the parent <code>Component</code>
 * @param message the message to display
 * @return <code>true</code> if the user pressed 'yes'
 */
public static boolean displayYesNoPrompt(Component parent, String message) {
    return (JOptionPane.YES_OPTION == JOptionPane.showOptionDialog(parent, message, null,
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null));
}

From source file:Main.java

public static boolean askUser(Component parent, String question) {
    int i = JOptionPane.showOptionDialog(parent, question, "Question", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, null, null);
    return i == JOptionPane.YES_OPTION;
}