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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
java_enthu
  • 2,279
  • 7
  • 44
  • 74
  • 2
    Don't use 'magic numbers' (e.g. the '2' in that method call). **Use the defined constants instead.** – Andrew Thompson Oct 14 '11 at 09:23
  • 1
    This [example](http://stackoverflow.com/questions/3002787/simple-popup-java-form-with-at-least-two-fields/3002830#3002830) may help you refine your question. – trashgod Oct 14 '11 at 10:03

4 Answers4

5

official tutorial contains example for that, another examples here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
3
/**
 * 
 * @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);
}
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).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @kleopatra Hmm.. good point. Answer edited to use a confirmation dialog. – Andrew Thompson Oct 14 '11 at 09:55
  • 1
    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.** – Andrew Thompson Oct 14 '11 at 09:56
  • deleted comment, pointing fingers at code-completion of my IDE – kleopatra Oct 14 '11 at 10:18
  • @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. – java_enthu Oct 14 '11 at 12:16
  • `JOptionPane.confirmationDialog.(....)` There is no such method. For more help, post an [SSCCE](http://pscode.org/sscce.html) of your best attempt. – Andrew Thompson Oct 14 '11 at 12:18
  • OK, but then, I also meant what I wrote when I added *"For more help, post an SSCCE of your best attempt."* – Andrew Thompson Oct 14 '11 at 13:09
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.

Sampath
  • 1,144
  • 1
  • 21
  • 38