Question

How do I create a scrollable container?

Answer

The good news is there is already a scrollable container as part of the java.awt package. It's called ScrollPane, and without this, you'd need to create one from scratch, using scrollbars. Phew!

Its easy to create an instance of ScrollPane, and to then add it to your applet or application. Simply create a new instance of ScrollPane, and then add it to your applet's canvas. Whatever size your applet is, the scroll pane will adjust to. Now as you add components, scroll bars will appear as needed. You can also change the size of the scrollbar via the setSize() method. Here's a quick example

// Create a scroll pane object
ScrollPane myContainer = new ScrollPane();
// Add scroll pane to my current canvas (if I am an applet)
add(myContainer);

// Now I can add components to my container......


Back