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:
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:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JFrameDemo extends JFrame { public JFrameDemo(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 450, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Grid of TextBoxes JPanel TextGrid = new JPanel(); GridLayout gl = new GridLayout(25,40); TextGrid.setLayout(gl); for (int i=1; i<1001; i++) TextGrid.add(new JTextField(String.valueOf(i),4)); |
4. Set JScrollPane for Text-Grid
Next, we construct the scrolling pane, and the below picture shows the parameters passed to it:
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:
1 2 3 4 5 6 |
//Sample 03: Create a Scroll Pane JScrollPane jsp = new JScrollPane( TextGrid, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); ControlHost.add(jsp); |
5. Youtube Demo
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JScrollPaneExample frame = new JScrollPaneExample("JScroll Pane Example"); frame.setVisible(true); } } |
6.2 JScrollPaneExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.ListModel; import javax.swing.ScrollPaneConstants; public class JScrollPaneExample extends JFrame { public JScrollPaneExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 450, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Grid of TextBoxes JPanel TextGrid = new JPanel(); GridLayout gl = new GridLayout(25,40); TextGrid.setLayout(gl); for (int i=1; i<1001; i++) TextGrid.add(new JTextField(String.valueOf(i),4)); //Sample 03: Create a Scroll Pane JScrollPane jsp = new JScrollPane( TextGrid, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); ControlHost.add(jsp); } } |
Categories: Swing
Tags: BorderLayout, GridLayout, JScrollPane, ScrollPaneConstants