Here's the code that creates the text area, makes it the scroll pane's client, and adds the scroll pane to a container
The boldface line of code creates the JScrollPane, specifying the text area as the scroll pane's client. The program doesn't invoke any methods on the JScrollPane object, since the scroll pane handles everything automatically: creating the scroll bars when necessary, redrawing the client when the user moves the scroll knobs, and so on.
You might have noticed that the preceding code sets the preferred size of the scroll pane's container. In the Java look and feel, this preferred size happens to be a bit less tall than required for the text area to display the 5 rows that we requested when creating it, so the scroll bar initially displays a vertical scroll bar. If we didn't restrict the size of the scroll pane's container, the scroll pane would be big enough for the text area to display the full 5 rows and 30 columns specified with the JTextArea constructor. Refer to Sizing a Scroll Pane for information about techniques for making a scroll pane the size you want.
The rest of this section discusses the following topics:
How a Scroll Pane Works
Setting the Scroll Bar Policy
Providing Custom Decorations
Implementing a Scrolling-Savvy Client
Sizing a Scroll Pane
Dynamically Changing the Client's Size
The Scroll Pane API
Examples that Use Scroll Panes