JaVa
   

The JTextArea

The JTextArea is similar to the JTextField in that it allows the user to enter text that we can retrieve from the object. The major difference is that the JTextArea allows the user to enter more than one line of input, just like a word processing app. The following example creates a JTextArea and allows the user to type into it. When the button in the example is pressed, all the text in the JTextArea is output to the console window. Let's look at this example now.

Code Listing 6: Using the JTextArea
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JTextAreaExample mainApp = new JTextAreaExample();
 }
 public JTextAreaExample()
 {
 super("JTextArea Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the textarea...
 textarea = new JTextArea();
 textarea.setBounds(10, 10, 270, 200);
 // Create a button...
 button = new JButton("Copy Text Area to Console Window");
 button.setBounds(10, 240, 270, 25);
 // Add the action listeners
 button.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(textarea);
 getContentPane().add(button);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == button)
 {
 System.out.println(textarea.getText());
 }
 }
 JButton button;
 JTextArea textarea;
}


Java End example

When we execute the code for this example, the app frame will be created and a JTextArea will be visible with a button shown below it. If we enter some text in the JTextArea, it will look as follows:

Java ScreenShot
Screenshot: Using the JTextArea

If we now click the Copy Text Area to Console Window button, we can see that the text we have entered into the JTextArea has been output to the console window. This can be seen in the following screen shot.

Java Click To expand
Screenshot: As you can see, the text in the JTextArea was output to the console when we clicked the button.

If we look at the code now, we can see that the JTextArea is no different than the JTextField in that we set the size of the JTextArea by using the setBounds method as follows:

textarea.setBounds(10, 10, 270, 200);


Then we simply add it to the content pane, as we have done with all our other GUI objects. Note in the actionPerformed method that we just use the getText method to retrieve all the text that the user has input into the JTextArea.

Using a JScrollPane with the JTextArea

One problem that you may have noticed is that when we enter more text than the JTextArea can hold on the screen, it does not automatically scroll for us. Let's look now at how we can add scroll bars to the JTextArea to allow the user to write more than can be displayed on the screen. Here is an example app that contains a text area with a vertical scroll bar:

Code Listing 7: Adding in a JTextArea to a JScrollPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JTextAreaExample mainApp = new JTextAreaExample();
 }
 public JTextAreaExample()
 {
 super("JTextArea Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the textarea...
 textarea = new JTextArea();
 textarea.setBounds(10, 10, 270, 200); // NEW ->
 // Create a scrollpane
 scrollPane = new JScrollPane(JScrollPane.VERTICAL_
 SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 scrollPane.setBounds(10, 10, 270, 200);
 scrollPane.add(textarea);
 scrollPane.setViewportView(textarea);
 // <- NEW
 // Create a button...
 button = new JButton("Copy Text Area to Console Window");
 button.setBounds(10, 240, 270, 25);
 // Add the action listeners
 button.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(scrollPane); // MODIFIED
 getContentPane().add(button);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == button)
 {
 System.out.println(textarea.getText());
 }
 }
 JButton button;
 JTextArea textarea;
 JScrollPane scrollPane; // NEW
}


Java End example

When we execute this example and add text to the JTextArea, we can see that the scroll bar allows us to move up and down the JTextArea so that we can view all the added text. Here is a screen shot of how this example looks.

Java ScreenShot
Screenshot: Using the JScrollPane with the JtextArea

Let's look at the code we have added to allow us to have a scroll bar for our JTextArea. First we create the JScrollPane with the following segment of code:

scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(10, 10, 270, 200);
scrollPane.add(textarea);
scrollPane.setViewportView(textarea);


Note in the constructor how we can set whether we wish to have either a vertical or horizontal scroll bar (or both) using the constants. Also notice how we add the textarea object to the scrollPane rather than the app's main content pane. Finally, we set the viewport view of the scrollPane to the textarea so it knows what it should be scrolling. Note that if only one component is added in the ScrollPane, the component to be viewed can be given directly for the constructor, and the setViewportView() method call is not required.

The only other change is where we used to add the JTextArea to the main content pane. Instead, we now add the JScrollPane object scrollPane, as it already contains our JTextArea object.

JaVa
   
Comments