JScrollPane

JScrollPane Swing example

1. Introduction

In this post, I’ll be giving an example of using JScrollPane Swing component. This component is usually used to create a scroller on panels that has more content that it can display. It will let the user scroll up, down, left or right, depending on what and where is the content of the panel is being viewed.

2. The example

For this example, we’ll be making a basic JScrollPane component wrapped in a UI with labels and buttons. The app will basically just display the description on a label underneath it.

Figure 1. JScroll pane example
Figure 1. JScroll pane example

The following code below is the entire source code that generates the sample above.

JScrollPaneSample.java

package com.jgc.areyes1.main;
package com.jgc.areyes1.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class JScrollPaneSample {
	public static void main(String[] args) {
		new JScrollPaneSample();
	}

	public JScrollPaneSample() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JTextArea textArea = new JTextArea();
				textArea.setText("areyes1\nareyes1\nareyes1\n"
						+ "areyes1\nareyes1\nareyes1\nareyes1\n"
						+ "areyes1\nareyes1\nareyes1\nareyes1\n"
						+ "areyes1\nareyes1\nareyes1\n");
				JScrollPane scrollPane = new JScrollPane(textArea);
				JFrame frame = new JFrame("JScrollPaneSample");
				frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(new Dimension(240, 180));
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}
}

Let’s go bits by bit in this example.

First of, We created a method that will basically initiate the creation of UI. The main() method calls the SwingUtilities.invokeLater before constructing the objects. This will create a new thread for the application upon constructing the overall components. When invoked, it will call the constructor that in turn will initialize the JScrollPane object. This will then create the following:

1. Initialize the JTextArea and set the text
2. Initialize the JScrollPane and put the text area (JTextArea) object in it.
3. Initialize the JFrame. This will be use to house the components
4. Add the JScrollPane object on the JFrame and set it’s size, location and visibility

3. Download the Eclipse project of this tutorial:

Download
You can download the full source code of this example here : jscrollpane-sample

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button