Java Swing How to - Put JTextArea message to JOptionPane.showMessageDialog








Question

We would like to know how to put JTextArea message to JOptionPane.showMessageDialog.

Answer

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*from www  . ja va 2  s  . co m*/
public class Main {
  public static void main(String[] args) {
    String text = "one two three four five six seven eight nine ten ";
    JTextArea textArea = new JTextArea(text);
    textArea.setColumns(30);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.setSize(textArea.getPreferredSize().width, 1);
    JOptionPane.showMessageDialog(null, new JScrollPane( textArea), "Not Truncated!",
        JOptionPane.WARNING_MESSAGE);
  }
}