Java Reference
In-Depth Information
Text Areas, Scrollbars, and Fonts
Text fields are useful for single-line text input, but they don't work well when the
user wants to type a larger or more complex message. Fortunately, there is another
kind of component called a text area that represents a text input box that can accept
multiple lines. Text areas are represented by JTextArea objects. You can construct a
JTextArea by passing the number of rows and columns (i.e., the number of lines and
the number of letters in each line):
JTextArea area = new JTextArea(5, 20);
The preceding line of code creates a text area that is 5 lines tall and 20 letters
wide:
The following program constructs a frame and adds a text area to it:
1 // Demonstrates the JTextArea component.
2
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class TextFrame {
7 public static void main(String[] args) {
8 JFrame frame = new JFrame();
9 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10 frame.setLayout( new FlowLayout());
11 frame.setSize( new Dimension(300, 150));
12 frame.setTitle("Text frame");
13
14 JTextArea area = new JTextArea(5, 20);
15 frame.add( area );
16
17 frame.setVisible( true );
18 }
19 }
The program produces the following graphical output (shown both before and
after the user types some text in the text area):
 
Search WWH ::




Custom Search