Java Reference
In-Depth Information
17.3 Text Areas
A JTextArea enables the user to enter multiple lines of text.
Key
Point
If you want to let the user enter multiple lines of text, you may create several instances of
JTextField . A better alternative is to use JTextArea , which enables the user to enter mul-
tiple lines of text. Figure 17.2 lists the constructors and methods in JTextArea .
javax.swing.text.JTextComponent
The get and set methods for these data fields are provided
in the class, but omitted in the UML diagram for brevity.
javax.swing.JTextArea
-columns: int
-rows: int
-tabSize: int
-lineWrap: boolean
The number of columns in this text area.
The number of rows in this text area.
The number of characters used to expand tabs (default: 8).
Indicates whether the line in the text area is automatically
wrapped (default: false ).
Indicates whether the line is wrapped on words or characters (default: false ).
Creates a default empty text area.
Creates an empty text area with the specified number of rows and columns.
Creates a new text area with the specified text displayed.
Creates a new text area with the specified text and number of rows and columns.
Appends the string to text in the text area.
Inserts string s in the specified position in the text area.
Replaces partial text in the range from position start to end with string s .
-wrapStyleWord: boolean
+JTextArea()
+JTextArea(rows: int, columns: int)
+JTextArea(text: String)
+JTextArea(text: String, rows: int, columns: int)
+append(s: String): void
+insert(s: String, pos: int): void
+replaceRange(s: String, start: int, end: int):
void
+getLineCount(): int
Returns the actual number of lines contained in the text area.
F IGURE 17.2 JTextArea enables you to enter or display multiple lines of characters.
Like JTextField , JTextArea inherits JTextComponent , which contains the methods
getText , setText , isEditable , and setEditable . You can specify whether a line is
wrapped in the lineWrap property. If lineWrap is true, you can specify how line is wrapped
in the wrapStyleWord property. If wrapStyleWord is true, line is wrapped on words. If it is
false, line is wrapped on characters. The following example creates a text area with 5 rows and
20 columns, line-wrapped on words, red foreground color, and Courier font, bold , 20 pixels.
JTextArea jtaNote = new JTextArea( "This is a text area" , 5 , 20 );
jtaNote.setLineWrap( true );
jtaNote.setWrapStyleWord( true );
jtaNote.setForeground(Color.red);
jtaNote.setFont( new Font( "Courier" , Font.BOLD, 20 ));
wrap line
wrap word
JTextArea does not handle scrolling, but you can create a JScrollPane object to hold an
instance of JTextArea and let JScrollPane handle scrolling for JTextArea , as follows:
// Create a scroll pane to hold text area
JScrollPane scrollPane = new JScrollPane(jtaNote);
add(scrollPane, BorderLayout.CENTER);
Tip
You can place any swing GUI component in a JScrollPane . JScrollPane provides
vertical and horizontal scrolling automatically if the component is too large to fit in the
viewing area.
JScrollPane
Listing 17.3 gives a program that displays an image and a text in a label, and a text in a text
area, as shown in Figure 17.3.
 
 
Search WWH ::




Custom Search