javacodex.com
Java Examples
Java Examples
memu home questions

JScrollPane

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically.

Source: (JScrollPaneExample.java)

import java.awt.*;
import javax.swing.*;
 
public class JScrollPaneExample extends JFrame {
 
  public JScrollPaneExample() {
 
    String str = "one two three four five six seven eight nine ten";
 
    JTextArea textArea = new JTextArea(str,10,25);
 
    // Configure the scroll bar policy
    int vsb = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    int hsb = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
 
    JScrollPane scrollPane = new JScrollPane(textArea,vsb,hsb);
 
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(scrollPane);
 
    this.setContentPane(panel);
    this.setTitle("JScrollPane Example");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
 
  }
 
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JScrollPaneExample();
    frame.setVisible(true);
  }
}
 

Output:

$ java JScrollPaneExample
Output



Contact: javacodex@yahoo.com