Output with JTextArea

Context

You want to display several lines of text in an applet or graphical application, perhaps to test a class or to give information to the user. Then use a JTextArea.

Example

package W04.TextInApplet;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class DisplayText extends JPanel
{  private JTextArea ta = new JTextArea(20, 40);

   public DisplayText()
   {  this.getContentFrame().add(ta);
      ta.append("Here's my message!\n");
   }
}

Pattern

package <packagename>;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class <name-of-class> extends JFrame
{  private JTextArea <name-of-textarea> = new TextArea(<optional-size-info>);

   public <name-of-class>()
   {  this.add(<name-of-textarea>);
      <name-of-textarea>.append(<text-to-display>);
   }
}

Notes

Related Patterns