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:
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:
Comments