A scrolling pane is a container that can be used to hold any component that can be scrolled. By default, the list and textarea component do not scroll automatically when number of items in the list or text area component go beyond the displayed area. So to make these components scroll, you must insert them into the scrollpane. After you create a scrollpane containing a component (list or textarea), the scrolling pane should be added to the container in place of that component. A scrolling pane can be created by instantiating the JScrollPane class.
import javax.swing.*;
import java.awt.*;
class JScrollPaneExample extends JFrame
{
JScrollPaneExample()
{
setLayout(new FlowLayout());
String[] language = {"Cobol","Fortran","Pascal","C","C++","java","C#"};
JLabel lblLanguage = new JLabel("Choose the Language");
JList LstMonth = new JList(language);
LstMonth.setVisibleRowCount (4);
JScrollPane s = new JScrollPane(LstMonth,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(lblLanguage);
add(s);
}
}
class JScrollPaneJavaExample
{
public static void main(String[] args)
{
JScrollPaneExample frame = new JScrollPaneExample();
frame.setTitle("JScrollPane Java Example");
frame.setBounds(200,250,180,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}