Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JScrollPane – Scrolling Text Grid Panel

1. About JScrollPane & View-Port

Java Swing provides JScrollPane to give scrolling support for the lengthy documents. The document can be a text, html documents or it can be a set of controls arranged in a container.

The Scroll Pane is a combination of JViewport and JScrollBar. We already have an idea about the JViewport, which is nothing but a porthole to view various portions of the document. The scrollbar helps in changing the focus of the porthole so that user can move through various parts of the document. Swing’s JScrollPane supports both Horizontal and Vertical scroll bars. Have a look at the below picture:

JScrollPane Scrollbar and JViewport
JScrollPane Scrollbar and JViewport

Here, the yellow portion shows the lengthy document. In a container, say in the JFrame, we cannot pack all the document’s content to view at one shot. It does not look good in a compressed view. Here comes the JScrollPane for rescue.

JScrollPane will make use of the JViewport. It moves the JViewport to view different document parts. The scroll bars will help in deciding in what direction the JScrollPane should move the view port. Note, the scroll pane can support Horizontal Scrollbar or Vertical Scrollbar or both.

2. About The Example

The example which we are going to create is below:

Java Swing JScrollPane Example
Java Swing JScrollPane Example

This example uses a grid of 1000 text boxes. Since we cannot view all the text boxes in the frame window, we give this grid of text boxes to the JScrollPane. Swing’s Scroll Pane component provides vertical & horizontal scroll bars and, using those, the user can scroll the grid and view any text box in the grid. In our example, we number the text boxes in a sequential order.

3. Create Text Grid via GridLayout

After setting-up the frame window with border layout, we create a panel with a Grid Layout. This grid layout is in 25×40 dimension. This means, each row contains 40 cells and likewise, there will be 25 rows. So, we will be having 1000 text boxes arranged in a grid. This grid acts as a huge document for our example. One can assume this as a yellow document, as shown in the first picture.

4. Set JScrollPane for Text-Grid

Next, we construct the scrolling pane, and the below picture shows the parameters passed to it:

JScrollpane Constructor
JScrollpane Constructor

The first param is the component which we want to scroll. In our case, it is a JPanel which consists of an array of text boxes packed in a grid format. The second and third params tell that the scrolling is required for both horizontal and vertical movement. Recall, these scroll bars are tied to a JViewport, which takes care which portion of the document to be viewed. After setting-up the JScrollPane with our text grid, we add the JScrollPane to the content pane of the Java Swing’s Frame Window. Below is the code snippet:

5. Youtube Demo

Java Swing JScrollPane – Demo

6. Code Reference

6.1 MainEntry.java

6.2 JScrollPaneExample.java

Categories: Swing

Tags: , , ,

Do you like this Example? Please comment about it for others!!

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