Aqua Phoenix
     >>  Lectures >>  Java 5  
 

Navigator
   
 
       
   

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);
....add(jScrollPane);
Figure 5.3
For JList:

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