Shows an input dialog that allows the user to enter multiple lines of text. - Java Swing

Java examples for Swing:JOptionPane

Description

Shows an input dialog that allows the user to enter multiple lines of text.

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{
    /**//from  w ww. j av  a  2 s  .c o m
     * Shows an input dialog that allows the user to enter multiple lines of text.
     * The window title is "Input".
     *
     * @param prompt the string to prompt the user with
     */
    public static String showMultilineInputDialog(final String prompt) {
        return showMultilineInputDialog("Input", prompt);
    }
    /**
     * Shows an input dialog that allows the user to enter multiple lines of text.
     * The window title can be specified.
     *
     * @param title  window title
     * @param prompt the String to prompt the user with
     */
    public static String showMultilineInputDialog(final String title,
            final String prompt) {
        return showMultilineInputDialog(title, prompt, "");
    }
    /**
     * Shows an input dialog that allows the user to enter multiple lines of text.
     * 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 showMultilineInputDialog(final String title,
            final String prompt, final String content) {
        return showMultilineInputDialog(parent, title, prompt, content);
    }
    /**
     * Shows an input dialog that allows the user to enter multiple lines of text.
     * 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 showMultilineInputDialog(final Component parent,
            final String title, final String prompt, final String content) {
        final JTextArea text = new JTextArea(content, 20, 40);
        JOptionPane pane = new JOptionPane(new Object[] { prompt,
                new JScrollPane(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