19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

644 Chapter 17 GUI Components<br />

Key<br />

Point<br />

17.3 Text Areas<br />

A JTextArea enables the user <strong>to</strong> enter multiple lines of text.<br />

If you want <strong>to</strong> let the user enter multiple lines of text, you may create several instances of<br />

JTextField. A better alternative is <strong>to</strong> use JTextArea, which enables the user <strong>to</strong> enter multiple<br />

lines of text. Figure 17.2 lists the construc<strong>to</strong>rs and methods in JTextArea.<br />

javax.swing.text.JTextComponent<br />

-columns: int<br />

-rows: int<br />

-tabSize: int<br />

-lineWrap: boolean<br />

javax.swing.JTextArea<br />

-wrapStyleWord: boolean<br />

+JTextArea()<br />

+JTextArea(rows: int, columns: int)<br />

+JTextArea(text: String)<br />

+JTextArea(text: String, rows: int, columns: int)<br />

+append(s: String): void<br />

+insert(s: String, pos: int): void<br />

+replaceRange(s: String, start: int, end: int):<br />

void<br />

+getLineCount(): int<br />

The get and set methods for these data fields are provided<br />

in the class, but omitted in the UML diagram for brevity.<br />

The number of columns in this text area.<br />

The number of rows in this text area.<br />

The number of characters used <strong>to</strong> expand tabs (default: 8).<br />

Indicates whether the line in the text area is au<strong>to</strong>matically<br />

wrapped (default: false).<br />

Indicates whether the line is wrapped on words or characters (default: false).<br />

Creates a default empty text area.<br />

Creates an empty text area with the specified number of rows and columns.<br />

Creates a new text area with the specified text displayed.<br />

Creates a new text area with the specified text and number of rows and columns.<br />

Appends the string <strong>to</strong> text in the text area.<br />

Inserts string s in the specified position in the text area.<br />

Replaces partial text in the range from position start <strong>to</strong> end with string s.<br />

Returns the actual number of lines contained in the text area.<br />

FIGURE 17.2<br />

JTextArea enables you <strong>to</strong> enter or display multiple lines of characters.<br />

Like JTextField, JTextArea inherits JTextComponent, which contains the methods<br />

getText, setText, isEditable, and setEditable. You can specify whether a line is<br />

wrapped in the lineWrap property. If lineWrap is true, you can specify how line is wrapped<br />

in the wrapStyleWord property. If wrapStyleWord is true, line is wrapped on words. If it is<br />

false, line is wrapped on characters. The following example creates a text area with 5 rows and<br />

20 columns, line-wrapped on words, red foreground color, and Courier font, bold, 20 pixels.<br />

wrap line<br />

wrap word<br />

JTextArea jtaNote = new JTextArea("This is a text area", 5, 20);<br />

jtaNote.setLineWrap(true);<br />

jtaNote.setWrapStyleWord(true);<br />

jtaNote.setForeground(Color.red);<br />

jtaNote.setFont(new Font("Courier", Font.BOLD, 20));<br />

JTextArea does not handle scrolling, but you can create a JScrollPane object <strong>to</strong> hold an<br />

instance of JTextArea and let JScrollPane handle scrolling for JTextArea, as follows:<br />

// Create a scroll pane <strong>to</strong> hold text area<br />

JScrollPane scrollPane = new JScrollPane(jtaNote);<br />

add(scrollPane, BorderLayout.CENTER);<br />

JScrollPane<br />

Tip<br />

You can place any swing GUI <strong>com</strong>ponent in a JScrollPane. JScrollPane provides<br />

vertical and horizontal scrolling au<strong>to</strong>matically if the <strong>com</strong>ponent is <strong>to</strong>o large <strong>to</strong> fit in the<br />

viewing area.<br />

Listing 17.3 gives a program that displays an image and a text in a label, and a text in a text<br />

area, as shown in Figure 17.3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!