How to add text area on JOptionPane
Asked Answered
G

4

6

Currently I have a JOptionPane. On clicking a button I am executing following line.

JOptionPane.showInputDialog(this,"Enter your message","Messages",2);

It opens a popup with a text box. This text box accepts around 40/50 characters. In fact my requirement is to take long message (upto 300 characters) in this text box which is stopping this. So we want to come up with a text area on this input dialog? Is it possible how? Is there any better solution? Thanks in advance.

Ginoginsberg answered 14/10, 2011 at 9:13 Comment(2)
Don't use 'magic numbers' (e.g. the '2' in that method call). Use the defined constants instead.Threequarter
This example may help you refine your question.Chlamys
G
5

official tutorial contains example for that, another examples here

General answered 14/10, 2011 at 9:18 Comment(0)
D
5
/**
 * 
 * @param obj
 * @param text
 * @param title
 * @return newText String
 */
public static String textAreaDialog(Object obj, String text, String title) {
    if(title == null) {
        title = "Your input";
    }
    JTextArea textArea = new JTextArea(text);
    textArea.setColumns(30);
    textArea.setRows(10);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setSize(textArea.getPreferredSize().width, textArea.getPreferredSize().height);
    int ret = JOptionPane.showConfirmDialog((Component) obj, new JScrollPane(textArea), title, JOptionPane.OK_OPTION);
    if (ret == 0) {
        return textArea.getText();
    } else {
        MyDialogs.Toast("Canceled by user\nChanges not saved", "Your choise");
    }
    return null;
}    

public static void Toast(Object msg, String title) {
    JOptionPane.showMessageDialog(null, msg, title, JOptionPane.OK_CANCEL_OPTION);
}
Definiens answered 14/4, 2019 at 17:29 Comment(0)
T
3

Put a JTextArea into JOptionPane.showConfirmDialog() (of type QUESTION_MESSAGE) then query the content of the text area once the dialog is disposed (and checking the return result to ensure the dialog was not cancelled by the user).

Threequarter answered 14/10, 2011 at 9:20 Comment(6)
@kleopatra Hmm.. good point. Answer edited to use a confirmation dialog.Threequarter
1) It is 'Swing', (singular, starting with a capital letter), not 'swings'. 2) I am not psychic, so I don't know what it is that you "don't get". Be specific.Threequarter
deleted comment, pointing fingers at code-completion of my IDE <g>Excise
@AndrewThompson : I created JTextArea object, and added to the JOptionPane.confirmationDialog.(....) but the return type of the method is int. Where as I want the text which is entered by the user.Ginoginsberg
JOptionPane.confirmationDialog.(....) There is no such method. For more help, post an SSCCE of your best attempt.Threequarter
OK, but then, I also meant what I wrote when I added "For more help, post an SSCCE of your best attempt."Threequarter
V
0

While I'm not sure whether using the JOptionPane is the best option for the purpose, you can add a text area, or any other form component into an OptionPane. Since you have asked specifically for a TextArea, I have added a JTextField and a JTextArea in the following example.

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Option Pane Text Area Example");

        final SpringLayout layout = new SpringLayout();

        final JPanel panel = new JPanel(layout);
        panel.setPreferredSize(new Dimension(250, 160));

        JLabel lblName = new JLabel("Name");
        panel.add(lblName);
        JTextField txtName = new JTextField(10);
        txtName.setBorder(BorderFactory.createLineBorder(Color.black));
        panel.add(txtName);

        JLabel lblAddress = new JLabel("Address");
        panel.add(lblAddress);
        JTextArea txtAddress = new JTextArea();
        txtAddress.setBorder(BorderFactory.createLineBorder(Color.black));
        txtAddress.setLineWrap(true);
        txtAddress.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(txtAddress,
                   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(250, 100));
        panel.add(scrollPane);

        layout.putConstraint(SpringLayout.WEST, lblName,
                             0,
                             SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, lblAddress,
                             10,
                             SpringLayout.SOUTH, lblName);

        layout.putConstraint(SpringLayout.WEST, txtName,
                             25,
                             SpringLayout.EAST, lblName);
        layout.putConstraint(SpringLayout.NORTH, scrollPane,
                             10,
                             SpringLayout.SOUTH, lblAddress);


        int result = JOptionPane.showConfirmDialog(frame, panel,
                "Text Box and Text Area Example", JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {
            System.out
                    .println(txtName.getText() + ",\n" + txtAddress.getText());
        } else {
            System.out.println("Canceled");
        }

        System.exit(0);
    }
}

However, I suggest its better you use a JDialog instead of the JOptionPane for complex input forms like these.

Vireo answered 8/3, 2015 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.