Shows an option dialog box. - Java Swing

Java examples for Swing:JOptionPane

Description

Shows an option dialog box.

Demo Code


import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URL;
import java.util.Arrays;

public class Main{
    private static final Config config = Config.getInstance();
    /**//  w  ww.  ja  v a 2 s.  co m
     * Shows an option dialog box.
     * The window title is "Input".
     *
     * @param prompt         the string to prompt the user with
     * @param possibleValues array of options to display
     */
    public static Object showOptionDialog(final String prompt,
            final Object[] possibleValues) {
        return showOptionDialog("Input", prompt, possibleValues);
    }
    /**
     * Shows an option dialog box.
     * The window title can be specified.
     *
     * @param title          window title
     * @param prompt         the string to prompt the user with
     * @param possibleValues array of options to display
     */
    public static Object showOptionDialog(final String title,
            final String prompt, final Object[] possibleValues) {
        return showOptionDialog(parent, title, prompt, possibleValues);
    }
    /**
     * Shows an option dialog box.
     * The window title can be specified.
     *
     * @param parent         the parent for the dialog
     * @param title          window title
     * @param prompt         the string to prompt the user with
     * @param possibleValues array of options to display
     */
    public static Object showOptionDialog(final Component parent,
            final String title, final String prompt,
            final Object[] possibleValues) {
        return JOptionPane.showInputDialog(parent, prompt, title,
                JOptionPane.QUESTION_MESSAGE, null, possibleValues,
                possibleValues[0]);
    }
    /**
     * Shows a dialog for obtaining a single line of input.
     * The window title is "Input".
     *
     * @param prompt the string to prompt the user with
     */
    public static String showInputDialog(final String prompt) {
        return showInputDialog("Input", prompt);
    }
    /**
     * Shows a dialog for obtaining a single line of input.
     * The window title can be specified.
     *
     * @param title  window title
     * @param prompt the string to prompt the user with
     */
    public static String showInputDialog(final String title,
            final String prompt) {
        return showInputDialog(parent, title, prompt);
    }
    /**
     * Shows a dialog for obtaining a single line of input.
     * The window title can be specified.
     *
     * @param title  window title
     * @param prompt the string to prompt the user with
     * @param parent the parent for the dialog
     */
    public static String showInputDialog(final Component parent,
            final String title, final String prompt) {
        if (!config.isGUIEnabled()) { //text only
            System.out.println("Input: " + title);
            System.out.println(prompt);
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    System.in));
            String result = null;
            while (result == null) {
                try {
                    result = in.readLine();
                } catch (IOException ioe) {
                    System.err.println(ioe.toString());
                }
            }
            return result;
        } else { //normal dialog
            JOptionPane pane = new JOptionPane(prompt,
                    JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.OK_CANCEL_OPTION);
            pane.setWantsInput(true);
            JDialog dialog = pane.createDialog(parent, title);
            dialog.pack();
            dialog.setVisible(true);
            Object value = pane.getInputValue();
            if (value == JOptionPane.UNINITIALIZED_VALUE) {
                return null;
            }
            return (String) value;
        }
    }
    /**
     * Shows a dialog for obtaining a single line of input.
     * The window title can be specified.
     *
     * @param title   window title
     * @param prompt  the string to prompt the user with
     * @param content the default contents of the input field
     */
    public static String showInputDialog(final String title,
            final String prompt, final String content) {
        return showInputDialog(parent, title, prompt, content);
    }
    /**
     * Shows a dialog for obtaining a single line of input.
     * The window title can be specified.
     *
     * @param parent  the parent for the dialog
     * @param title   window title
     * @param prompt  the string to prompt the user with
     * @param content the default contents of the input field
     */
    public static String showInputDialog(final Component parent,
            final String title, final String prompt, final String content) {
        final JTextField text = new JTextField(content, 30);
        JOptionPane pane = new JOptionPane(new Object[] { prompt, text },
                JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
        pane.setWantsInput(false);
        JDialog dialog = pane.createDialog(parent, title);
        dialog.pack();
        dialog.setVisible(true);
        Integer value = (Integer) pane.getValue();
        if (value == null || value.intValue() == JOptionPane.CANCEL_OPTION
                || value.intValue() == JOptionPane.CLOSED_OPTION) {
            return null;
        }
        return text.getText();
    }
}

Related Tutorials