Example usage for javax.swing JOptionPane showInputDialog

List of usage examples for javax.swing JOptionPane showInputDialog

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public static Object showInputDialog(Component parentComponent, Object message, String title, int messageType,
        Icon icon, Object[] selectionValues, Object initialSelectionValue) throws HeadlessException 

Source Link

Document

Prompts the user for input in a blocking dialog where the initial selection, possible selections, and all other options can be specified.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComponent parentComponent = null;
    Object message = "Please select";
    String title = "JOptionPane Input Dialog";
    int messageType = JOptionPane.INFORMATION_MESSAGE;
    Icon icon = null;/*from   w  w w .j  a  v a  2  s. c o m*/
    Object[] selectionValues = new String[] { "A", "B", "C" };
    Object initialSelectionValue = selectionValues[2];
    Object response = JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon,
            selectionValues, initialSelectionValue);

    if (response == null) {
        System.out.println("we have  cancelled the   input dialog.");
    } else {
        System.out.println("we entered:  " + response);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JOptionPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Choose One?", "JOptionPane Sample",
                    JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C" }, "B");
            System.out.println("Response: " + response);
        }/*  w  w  w . jav a 2s.com*/
    };
    button.addActionListener(actionListener);
    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:SamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Sample 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();
            Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?",
                    "Select a Destination", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "B");
            System.out.println("Response: " + response);
        }//from  w  ww  .  j  av  a  2s. com
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:SamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Sample 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();
            Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?",
                    "Select a Destination", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "McDonalds");
            System.out.println("Response: " + response);
        }//  ww w  .j a v a  2s.  co m
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:OptionPaneSample.java

public static void main(String args[]) {
    JFrame f = new JFrame("JOptionPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Where do you want to go tomorrow?",
                    "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "E");
            System.out.println("Response: " + response);
        }//w  w w.j a v  a  2 s  .  c  o  m
    };
    button.addActionListener(actionListener);
    content.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Show input dialog in item selection mode with specified title, message
 * and initial selection./*from w w  w  .jav  a 2  s  .  co m*/
 * 
 * @param parent
 *            the parent component of the input dialog, set {@code null} if
 *            not has one
 * @param title
 *            the title of the dialog
 * @param message
 *            the message to display
 * @param initial
 *            an array of <code>Object</code>s that
 *            gives the possible selections
 * @param selections
 *            the value used to initialize the input
 *            field
 * @return the selected item, or <code>null</code> if user canceled input.
 */
public static Object selectDialog(Component parent, String title, String message, Object initial,
        Object[] selections) {
    return JOptionPane.showInputDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE, null,
            selections, initial);
}

From source file:Main.java

/**
 * Mostra uma caixa de menssagem para que seja ensirido um valor.
 * @param frame/*  ww w  . j a  va  2  s.  c  om*/
 * @param texto
 * @param title
 * @param valorInicial
 * @param type
 * @return
 * @author Thiago Benega
 * @since 13/04/2009
 */
public static String showTextboxDialog(javax.swing.JFrame frame, String texto, String title,
        String valorInicial, int type) {
    Object txt = null;
    JDialog jDialog = new JDialog();
    jDialog.setTitle(title);
    jDialog.setFocusableWindowState(true);

    if (frame != null) {
        frame.setExtendedState(Frame.ICONIFIED);
        frame.pack();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    switch (type) {
    case JOptionPane.OK_CANCEL_OPTION:
        txt = JOptionPane.showInputDialog(jDialog, texto, title, type, null, null, valorInicial);//.toString();
        break;
    case JOptionPane.YES_NO_OPTION:
        txt = JOptionPane.showConfirmDialog(jDialog, texto, title, type, JOptionPane.INFORMATION_MESSAGE);
        break;
    default:
        JOptionPane.showMessageDialog(jDialog, texto, title, type);
        break;
    }

    jDialog = null;
    return txt != null ? txt.toString() : null;

}

From source file:Main.java

/**
 * Show input dialog in item selection mode with specified title, message
 * and initial selection./*  ww w. j  a v a  2  s.c  o m*/
 * 
 * @param parent
 *            the parent component of the input dialog, set {@code null} if
 *            not has one
 * @param title
 *            the title of the dialog
 * @param message
 *            the message to display
 * @param initial
 *            an array of <code>Object</code>s that
 *            gives the possible selections
 * @param selections
 *            the value used to initialize the input
 *            field
 * @return the index of the selected item, or <code>-1</code> if user
 *         canceled input.
 */
public static int selectIndexDialog(Component parent, String title, String message, Object initial,
        Object[] selections) {
    Object obj = JOptionPane.showInputDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE, null,
            selections, initial);
    if (obj != null)
        for (int i = 0; i < selections.length; i++)
            if (obj.equals(selections[i]))
                return i;
    return -1;
}

From source file:Main.java

/**
 * Show input dialog with specified title, message and initial input
 * content./*from   www . j a  v  a  2  s  . c  om*/
 * 
 * @param parent
 *            the parent component of the input dialog, set {@code null} if
 *            not has one
 * @param title
 *            the title of the dialog
 * @param message
 *            the message to display
 * @param initial
 *            the initial input content
 * @return the input string, may be an empty string
 */
public static String inputDialog(Component parent, String title, String message, String initial) {
    Object obj = JOptionPane.showInputDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE, null,
            null, initial);
    if (obj == null)
        return null;
    else
        return obj.toString();
}

From source file:au.org.ala.delta.ui.MessageDialogHelper.java

/**
 * Uses JOptionPane.showInputDialog to display the supplied (multi-line) message and return the
 * user input./*from   w w w  . ja v  a2  s.c  o m*/
 * @param parent the parent component used to display the JOptionPane.
 * @param title the title for the option pane.
 * @param text the message text to display on the option pane.  Multi-line messages should 
 * use the "\n" character.
 * @param numColumns the column position to wrap the text at.
 * @param initialValue the initial value for the user input.
 * @return the value supplied to the JOptionPane input dialog.
 */
public static String showInputDialog(Component parent, String title, String text, int numColumns,
        String initialValue) {

    JTextArea messageDisplay = createMessageDisplay(text, numColumns);
    return (String) JOptionPane.showInputDialog(parent, messageDisplay, title, JOptionPane.PLAIN_MESSAGE, null,
            null, initialValue);
}