JScrollPane Example
This JScrollPane example provides user control over what is visible when the content is larger than the component it has been added to. The component is added into the JScrollPane which then looks after the management of the scrollbars.
You will notice that the vertical scrollbar can be made to appear as you reduce the size of the JScrollPane, and disappear when it is increased above a certain size.
Here it appears because the last item of the list is not shown in the JScrollPane.
// JScrollPane Example
import java.awt.*;
import javax.swing.*;
class JScrollPaneExample {
public static void main(String[] args) {
String[] sa = {
"Snoopy","Timmy","Rin Tin Tin","Deputy Dawg","Lassie",
"Pluto","Goofy","Shep","Scooby Doo","Dogmatix"
};
JPanel p = new JPanel(new GridLayout(0,1));
for(int i=0;i<sa.length;++i) p.add(new JLabel(sa[i]));
JScrollPane jscrollpane = new JScrollPane(p);
JFrame f = new JFrame("JScrollPane");
f.getContentPane().add(jscrollpane);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100, 150);
f.setVisible(true);
}
}
