JScrollPane

Set scrollbar policy in JScrollPane container

With this example we are going to see how to set scroll bar policy in a JScrollPane container. This is very useful when you want to further customize your scroll bars.

Basically all you have to do to set scroll bar policy in a JScrollPane is:

  • Create a new JFrame.
  • Create a new JTextArea.
  • Create a new JScrollPane with the above text area.
  • Use setHorizontalScrollBarPolicy, setVerticalScrollBarPolicy to set the scroll bar policy.

 
Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SetScrollbarPolicyInJScrollPaneContainer {

	private static final long serialVersionUID = 1L;

	private static void createAndShowGUI() {

		// Create and set up the window.
		final JFrame frame = new JFrame("Scroll Pane Example");

		// Display the window.
		frame.setSize(200, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// set flow layout for the frame
		frame.getContentPane().setLayout(new FlowLayout());

		JTextArea textArea = new JTextArea(5, 5);
		JScrollPane scrollableTextArea = new JScrollPane(textArea);

		// determine when the horizontal scrollbar appears in the scrollpane
		int horizontalPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		// int horizontalPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
		// int horizontalPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;

		// determine when the vertical scrollbar appears in the scrollpane
		int vericalPolicy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
		// int vericalPolicy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
		// int vericalPolicy = JScrollPane.VERTICAL_SCROLLBAR_NEVER;

		scrollableTextArea.setHorizontalScrollBarPolicy(horizontalPolicy);
		scrollableTextArea.setVerticalScrollBarPolicy(vericalPolicy);

		frame.getContentPane().add(scrollableTextArea);

	}

	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

 
This was an example on how to set scroll bar policy in a JScrollPane container.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button