JTextArea

A JTextArea provides an area for manipulating multiple lines of text. Like class JTextField, JTextArea is a subclass of JTextComponent, which declares common methods for JTextFields, JTextAreas and several other text-based GUI components.

The application in Fig. 11.47 and Fig. 11.48 demonstrates JTextAreas. One JTextArea displays text that the user can select. The other JTextArea is uneditable and is used to display the text the user selected in the first JTextArea. Unlike JTextFields, JTextAreas do not have action events. As with multiple-selection JLists (Section 11.12), an external event from another GUI component indicates when to process the text in a JTextArea. For example, when typing an e-mail message, you normally click a Send button to send the text of the message to the recipient. Similarly, when editing a document in a word processor, you normally save the file by selecting a Save or Save As... menu item. In this program, the button Copy >>> generates the external event that copies the selected text in the left JTextArea and displays it in the right JTextArea.

Figure 11.47. Copying selected text from one JTextArea to another.

(This item is displayed on page 579 in the print version)

 1 // Fig. 11.47: TextAreaFrame.java
 2 // Copying selected text from one textarea to another.
 3 import java.awt.event.ActionListener;
 4 import java.awt.event.ActionEvent;
 5 import javax.swing.Box;
 6 import javax.swing.JFrame;
 7 import javax.swing.JTextArea;
 8 import javax.swing.JButton;
 9 import javax.swing.JScrollPane;
10
11 public class TextAreaFrame extends JFrame
12 {
13 private JTextArea textArea1; // displays demo string 
14 private JTextArea textArea2; // highlighted text is copied here
15 private JButton copyJButton; // initiates copying of text
16
17 // no-argument constructor
18 public TextAreaFrame()
19 {
20 super( "TextArea Demo" );
21 Box box = Box.createHorizontalBox(); // create box
22 String demo = "This is a demo string to
" +
23 "illustrate copying text
from one textarea to 
" +
24 "another textarea using an
external event
";
25
26 textArea1 = new JTextArea( demo, 10, 15 ); // create textarea1
27 box.add( new JScrollPane( textArea1 ) ); // add scrollpane 
28
29 copyJButton = new JButton( "Copy >>>" ); // create copy button
30 box.add( copyJButton ); // add copy button to box
31 copyJButton.addActionListener(
32
33 new ActionListener() // anonymous inner class
34 {
35 // set text in textArea2 to selected text from textArea1
36 public void actionPerformed( ActionEvent event )
37 {
38 textArea2.setText( textArea1.getSelectedText() );
39 } // end method actionPerformed
40 } // end anonymous inner class
41 ); // end call to addActionListener
42
43 textArea2 = new JTextArea( 10, 15 ); // create second textarea
44 textArea2.setEditable( false ); // disable editing 
45 box.add( new JScrollPane( textArea2 ) ); // add scrollpane 
46
47 add( box ); // add box to frame
48 } // end TextAreaFrame constructor
49 } // end class TextAreaFrame

Figure 11.48. Test class for TextAreaFrame.

(This item is displayed on page 580 in the print version)

 1 // Fig. 11.48: TextAreaDemo.java
 2 // Copying selected text from one textarea to another.
 3 import javax.swing.JFrame;
 4
 5 public class TextAreaDemo
 6 {
 7 public static void main( String args[] )
 8 {
 9 TextAreaFrame textAreaFrame = new TextAreaFrame();
10 textAreaFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11 textAreaFrame.setSize( 425, 200 ); // set frame size
12 textAreaFrame.setVisible( true ); // display frame
13 } // end main
14 } // end class TextAreaDemo
 

In the constructor (lines 1848), line 21 creates a Box container (package javax.swing) to organize the GUI components. Box is a subclass of Container that uses a BoxLayout layout manager (discussed in detail in Section 22.9) to arrange the GUI components either horizontally or vertically. Box's static method createHorizontalBox creates a Box that arranges components from left to right in the order that they are attached.

Lines 26 and 43 create JTextAreas textArea1 and textArea2. Line 26 uses JTextArea's three-argument constructor, which takes a String representing the initial text and two ints specifying that the JTextArea has 10 rows and 15 columns. Line 43 uses JTextArea's two-argument constructor, specifying that the JTextArea has 10 rows and 15 columns. Line 26 specifies that demo should be displayed as the default JTextArea content. A JTextArea does not provide scrollbars if it cannot display its complete contents. So, line 27 creates a JScrollPane object, initializes it with textArea1 and attaches it to container box. By default, horizontal and vertical scrollbars will appear as necessary in a JScrollPane.

Lines 2941 create JButton object copyButton with the label "Copy >>>", add copyButton to container box and register the event handler for copyButton's ActionEvent. This button provides the external event that determines when the program should copy the selected text in textArea1 to textArea2. When the user clicks copyButton, line 38 in actionPerformed indicates that method getSelectedText (inherited into JTextArea from JTextComponent) should return the selected text from textArea1. The user selects text by dragging the mouse over the desired text to highlight it. Method setText changes the text in textArea2 to the string returned by getSelectedText.

Lines 4345 create textArea2, set its editable property to false and add it to container box. Line 47 adds box to the JFrame. Recall from Section 11.17 that the default layout of a JFrame is a BorderLayout and that the add method by default attaches its argument to the CENTER of the BorderLayout.

It is sometimes desirable, when text reaches the right side of a JTextArea, to have the text wrap to the next line. This is referred to as line wrapping. By default, JTextArea does not wrap lines.

Look-and-Feel Observation 11.20

To provide line-wrapping functionality for a JTextArea, invoke JTextArea method setLineWrap with a TRue argument.

JScrollPane Scrollbar Policies

This example uses a JScrollPane to provide scrolling for a JTextArea. By default, JScrollPane displays scrollbars only if they are required. You can set the horizontal and vertical scrollbar policies of a JScrollPane when it is constructed. If a program has a reference to a JScrollPane, the program can use JScrollPane methods setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy to change the scrollbar policies at any time. Class JScrollPane declares the constants

 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS

to indicate that a scrollbar should always appear, constants

 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

to indicate that a scrollbar should appear only if necessary (the defaults) and constants

 JScrollPane.VERTICAL_SCROLLBAR_NEVER
 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER

to indicate that a scrollbar should never appear. If the horizontal scrollbar policy is set to JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, a JTextArea attached to the JScrollPane will automatically wrap lines.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net