[ Back | Previous | Next ]

How to create a ScrollPane?

Package:
java.awt.*
Product:
JDK
Release:
1.1.x
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
The ScrollPane is very handy for graphical Components. But how to make use of it. 
My first mistake was the presumption that the ScrollPane had a layout and that more components could be added. 
Finally I've made this sample to make me ScrollPane aware.
// TestScrollPane() 
import java.awt.*; 

public class TestScrollPane extends java.applet.Applet { 
   public void init() { 
      ScrollPane sp = new ScrollPane( ScrollPane.SCROLLBARS_ALWAYS ); 
      Panel scrollpanel = new Panel(); 
      scrollpanel.setLayout( new GridLayout(20,1) ); 
      add(sp); 
      for (int i=0; i < 20; i++) { 
         scrollpanel.add( new Label( "String " + i), i ); 
      } 
      sp.add( scrollpanel ); 
      sp.doLayout(); 
   } 
} 
1