If you want to let the user enter multiple lines of text, you have to create several instances of JTextField . A better alternative is to use JTextArea , which enables the user to enter multiple lines of text. Figure 15.18 lists the constructors and methods in JTextArea .
Like JTextField , JTextArea inherits JTextComponent , which contains the methods getText , setText , isEditable , and setEditable . Here is an example of creating a text area with five rows and twenty 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 ));
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(jta = new JTextArea()); add(scrollPane, BorderLayout.CENTER);
Listing 15.6 gives a program that displays an image and a text in a label, and a text in a text area, as shown in Figure 15.19.
Here are the major steps in the program:
1. | Create a class named DescriptionPanel that extends JPanel . This class contains a text area inside a scroll pane, a label for displaying an image icon, and a title. This class is used in the present example and will be reused in later examples. |
| |
2. | Create a class named TextAreaDemo that extends JFrame . Create an instance of DescriptionPanel and add it to the center of the frame. The relationship between DescriptionPanel and TextAreaDemo is shown in Figure 15.20. Figure 15.20. TextAreaDemo uses DescriptionPanel to display an image, title, and text description of a national flag.
|
1 import java.awt.*; 2 import javax.swing.*; 3 4 public class TextAreaDemo extends JFrame { 5 // Declare and create a description panel 6 private DescriptionPanel descriptionPanel = new DescriptionPanel(); 7 8 public static void main(String[] args) { 9 TextAreaDemo frame = new TextAreaDemo(); 10 frame.pack(); 11 frame.setLocationRelativeTo( null ); // Center the frame 12 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13 frame.setTitle( "TextAreaDemo" ); 14 frame.setVisible( true ); 15 } 16 17 public TextAreaDemo() { 18 // Set title, text and image in the description panel 19 descriptionPanel.setTitle( "Canada" ); 20 String description = "The Maple Leaf flag \n\n" + 21 "The Canadian National Flag was adopted by the Canadian " + 22 "Parliament on October 22, 1964 and was proclaimed into law " + 23 "by Her Majesty Queen Elizabeth II (the Queen of Canada) on " + 24 "February 15, 1965. The Canadian Flag (colloquially known " + 25 "as The Maple Leaf Flag) is a red flag of the proportions " + 26 "two by length and one by width, containing in its center a " + 27 "white square, with a single red stylized eleven-point " + 28 "mapleleaf centered in the white square." ; 29 descriptionPanel.setDescription(description); 30 descriptionPanel.setImageIcon( new ImageIcon( "image/ca.gif" )); 31 32 // Add the description panel to the frame 33 setLayout( new BorderLayout()); 34 add(descriptionPanel, BorderLayout.CENTER); 35 } 36 } 37 38 // Define a panel for displaying image and text 39 class DescriptionPanel extends JPanel { 40 /** Label for displaying an image icon and a text */ 41 private JLabel jlblImageTitle = new JLabel(); 42 43 /** Text area for displaying text */ 44 private JTextArea jtaDescription = new JTextArea(); 45 46 public DescriptionPanel() { 47 // Center the icon and text and place the text under the icon 48 jlblImageTitle.setHorizontalAlignment(JLabel.CENTER); 49 jlblImageTitle.setHorizontalTextPosition(JLabel.CENTER); 50 jlblImageTitle.setVerticalTextPosition(JLabel.BOTTOM); 51 52 // Set the font in the label and the text field 53 jlblImageTitle.setFont( new Font( "SansSerif" , Font.BOLD, 16 )); 54 jtaDescription.setFont( new Font( "Serif" , Font.PLAIN, 14 )); 55 56 // Set lineWrap and wrapStyleWord true for the text area 57 jtaDescription.setLineWrap( true ); 58 jtaDescription.setWrapStyleWord( true ); 59 jtaDescription.setEditable( false ); 60 61 // Create a scroll pane to hold the text area 62 JScrollPane scrollPane = new JScrollPane(jtaDescription); 63 64 // Set BorderLayout for the panel, add label and scrollpane 65 setLayout( new BorderLayout( 5 , 5 )); 66 add(scrollPane, BorderLayout.CENTER); 67 add(jlblImageTitle, BorderLayout.WEST); 68 } 69 70 /** Set the title */ 71 public void setTitle(String title) { 72 jlblImageTitle.setText(title); 73 } 74 75 /** Set the image icon */ 76 public void setImageIcon(ImageIcon icon) { 77 jlblImageTitle.setIcon(icon); 78 } 79 80 /** Set the text description */ 81 public void setDescription(String text) { 82 jtaDescription.setText(text); 83 } 84 } |
TextAreaDemo simply creates an instance of DescriptionPanel (line 6), and sets the title (line 19), image (line 30), and text in the description panel (line 29). DescriptionPanel is a subclass of JPanel . DescriptionPanel contains a label for displaying an image icon and a text title, and a text area for displaying a description of the image.
It is not necessary to create a separate class for DescriptionPanel in this example. Nevertheless, this class was created for reuse in the next example, where you will use it to display a description panel for various images.
The text area is inside a JScrollPane (line 62), which provides scrolling functions for the text area. Scroll bars automatically appear if there is more text than the physical size of the text area, and disappear if the text is deleted and the remaining text does not exceed the text area size .
The lineWrap property is set to true (line 57) so that the line is automatically wrapped when the text cannot fit in one line. The wrapStyleWord property is set to true (line 58) so that the line is wrapped on words rather than characters. The text area is set non-editable (line 59), so you cannot edit the description in the text area.