开发者

How to add text area on JOptionPane

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.


official tutorial contains example for that, another examples here


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).


/**
 * 
 * @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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜