You are here:   HomeJava ProgrammingSwingJViewport in Java Swing Example
by Dinesh Thakur Category: Swing

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);
    }
 }

JScrollPane in Java Swing Example





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article