Java programming course: 14.4 JTextArea


JTextArea

Whereas JTextField allows the user to enter text on a single line, the JTextArea class allows entry of text in a box consisting of multiple lines, which is useful for entering information such as an address, for example:

JTextArea area = new JTextArea(4, 30);
panel.add(area);
 
  • The constructor's first argument is the number of rows to display, and the second argument the number of columns to display

Like JTextField, you can supply some default text using a three-argument constructor:

JTextArea area = new JTextArea("Default text", 4, 30); 

The frame should look as follows:

JTextArea

To handle the possibility of the user typing text which extends beyond the number of rows or columns, it is advisable to nest the JTextArea object within a JScrollPane. The JScrollPane object will monitor the text within the nested component and automatically display horizontal and/or vertical scroll bars if necessary:

JTextArea area = new JTextArea("Default text", 4, 30);
panel.add(new JScrollPane(area));
 

Try entering some text on multiple lines and notice the effect:

Scrollable JTextArea


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Thursday, 11 December 2025

Captcha Image