|
Java(c) How to Program Authors: Deitel H. M. Published year: 2003 Pages: 247/615 |
11.19. JTextAreaA 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 , JTextArea s and several other text-based GUI components . The application in Fig. 11.47 and Fig. 11.48 demonstrates JTextArea s. 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 , JTextArea s 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)
Figure 11.48. Test class for TextAreaFrame .(This item is displayed on page 580 in the print version)
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 JTextArea s textArea1 and textArea2 . Line 26 uses JTextArea 's three-argument constructor, which takes a String representing the initial text and two int s 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
JScrollPane Scrollbar PoliciesThis 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. |
|
Java(c) How to Program Authors: Deitel H. M. Published year: 2003 Pages: 247/615 |
![]() C++ How to Program (4th Edition) | ![]() Java How to Program, 7th Edition | ![]() Ivor Horton's Beginning Visual C++ 2008 | ![]() Head First Java |