In effect, a JTextArea component can display multiple lines of text. A text area can be used for either input or output.
The JTextArea class is included in the statement: import javax.swing.*;
JTextArea(int rows, int columns) JTextArea(String text, int rows, int columns)
The rows parameter specifies the number of rows of text to display in the text area.
The columns parameter specifies the width of the field. The width of the text field (in columns) is an abstract measure and does not necessarily indicate the number of characters that can be displayed in one row of text.
The text parameter specifies the text that will be displayed.
append(String text)
Add text to the end of the existing text.
String getText()
Return the text currently displayed in the text area.
requestFocus()
Make the text area the active component.
setEditable(boolean b)
If the parameter value is true then user can edit the text. Otherwise, the user cannot.
setFocusable(boolean b)
If the parameter value is true then the text area can receive the focus. Otherwise, it cannot.
setFont(Font f)
Specify the font to use.
setText(String text)
Specify the text to be displayed.
The size of an empty JTextArea component is determined by the rows and columns parameters used in its constructor. Typically, the component will be high enough to display the specified number of rows of text. The width will be such that it can display roughly the same number of characters as specified by the columns parameter. The width is determined using an average character width and the actual number of characters that can be displayed will depend an the font and the specific text.
However, the size of a JTextArea component changes dynamically if a line of text is too long to fit within the width of the component or if there are too many lines of text to fit. In either case, the size of the component will automatically adjust to fit the text. In general, this is an extremely undesirable behavior.
A JTextArea component should only be used in conjunction with a JScrollPane container. Placing a JTextArea component within a JScrollPane object, ensures that the JTextArea component's size will remain fixed. If the text exceeds the bounds of the component, scrollbars permit the user to scroll horizontally or vertically, as necessary.
Using a JScrollPane object is a three-step process: