Java Swing How to - Create JScrollPane and set scroll bar policy








Question

We would like to know how to create JScrollPane and set scroll bar policy.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/* w  w  w.jav  a 2s.  c o m*/
public class Main {

  public static void main(String[] args) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    JTextArea textArea = new JTextArea(25, 30);

    JScrollPane textScroll = new JScrollPane(textArea,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    window.add(textScroll);
    window.pack();
    window.setVisible(true);
  }
}