O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

JScrollPane with a list too long to be shown

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[] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required