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.