Aqua Phoenix
     >>  Lectures >>  Java 5  


5.6 JScrollPane

The JScrollPane takes somewhat of a special role in Swing. Unlike in AWT, where TextArea and List are by default scrollable (i.e. they have scrollbars), JTextArea, JList, and other Swing components do not. This is a design attempt to create a generic Scrollable interface and any GUI component that implements Scrollable can be added to a ScrollPane.

Simply instantiating a JTextArea or JList does not render them scrollable. The following additional call to the JScrollPane object are required to add scrollbars:

JScrollPane jScrollPane = new JScrollPane(SOME_SCROLLABLE_GUI_COMPONENT);
For JTextArea:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(jTextArea);
Figure 5.3
For JList:

JList jList = new JList(4);
JScrollPane jScrollPane = new JScrollPane(jList);
Figure 5.4