The JScrollPane Class
The JScrollPane
class offers a more flexible version of
the ScrollPane class found in the AWT package.
Beyond the automatic scrollbars, you can put in horizontal and
vertical headers as well as active components in the corners of your
pane. (Figure 11.7, later in the chapter, shows the
exact areas available in a JScrollPane, which is
managed by the ScrollPaneLayout class.)
Many of the Swing components you have seen make use of the
JScrollPane to handle their scrolling. The
JList component, for example, does not handle
scrolling on its own. Instead, it concentrates on presenting the list
and making selection easy, assuming you’ll put it inside a
JScrollPane if you need scrolling.
Figure 11.3 shows a simple
JScrollPane in action with a
JList object.

Figure 11-3. JScrollPane with a list too long to be shown
This particular example does not take advantage of the row or column headers. The scroll pane adds the scrollbars automatically, but only “as needed.” If we were to resize the window to make it much larger, the scrollbars would disappear. Here’s the code that builds this pane:
// ScrollList.java
// A simple JScrollPane. // import javax.swing.*; import java.awt.*; public class ScrollList extends JFrame { JScrollPane scrollpane; public ScrollList() { super("JScrollPane Demonstration"); setSize(300, 200); addWindowListener(new BasicWindowMonitor()); String categories[] ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access