Java Swing How to - Set JScrollPane ScrollBar Policy








Question

We would like to know how to set JScrollPane ScrollBar Policy.

Answer

import java.awt.Dimension;
/* w w w.j a v  a  2  s . c o  m*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main extends JPanel {
  private JScrollPane vertical;
  private JTextArea console;

  public Main() {
    setPreferredSize(new Dimension(200, 250));
    console = new JTextArea(15, 15);

    vertical = new JScrollPane(console);
    vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    add(vertical);
  }

  public static void main(String args[]) {
    new JFrame() {
      {
        getContentPane().add(new Main());
        pack();
        setVisible(true);
      }
    };
  }
}