Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A ScrollPane can be used to make other components scrollable. ScrollPanes should be used to contain components that are bigger than their allocated screen space. In the example below, a ScrollPane has been placed around a TextArea.
TextArea_Scrollable_Demo example: (Run Applet)
import java.awt.*;
import javax.swing.*;
public class TextArea_Scrollable_Demo extends JApplet
{
@Override
public void init()
{
this.setContentPane(new View());
}
public class View extends JPanel
{
public View()
{
final JTextArea textArea = new JTextArea();
final JScrollPane scrollPane = new JScrollPane(textArea); // make area1 scrollable
this.setLayout(new GridLayout(1, 1));
this.add(scrollPane); // add the scrollable area
textArea.append("When there is too much text to be display in the JTextArea, the JScrollPane automatically produces scroll bars.");
}
}
}
What other components could be usefully placed inside a ScrollPane? Explain why ScrollPanes would be useful in these cases.
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.