In this tutorial

You will learn how to...
  • How to place things on a JScrollPane.
  • How to define scrollbar behaviour.

JScrollPane

JScrollPanes are used in GUI development to restrict a widget to a certain size on the screen, then provide a way of scrolling up and down, left and right if the widget becomes any bigger than the 'Viewport' size.

This is used primarily in JTextArea, JTable and JList where the data displayed by the widget is changed dynamically and can really affect the size and dimensions of the widget.

In this example, we display a JTextArea with a passage of text from 'The History of Swing' from Wikipedia. We use a ScrollPane to give the user access to the entire passage, even though we cannot display it all on the GUI.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import javax.swing.*;
import java.awt.Dimension;

public class ScrollPaneExample{

    public JPanel createContentPane (){

        // As usual, we create our bottom-level panel.
        JPanel totalGUI = new JPanel();
        
        // This is the story we took from Wikipedia.
        String story = "The Internet Foundation Classes (IFC) were a graphics "+
                       "library for Java originally developed by Netscape Communications "+
                       "Corporation and first released on December 16, 1996.\n\n"+
                       "On April 2, 1997, Sun Microsystems and Netscape Communications"+
                       " Corporation announced their intention to combine IFC with other"+
                       " technologies to form the Java Foundation Classes. In addition "+
                       "to the components originally provided by IFC, Swing introduced "+
                       "a mechanism that allowed the look and feel of every component "+
                       "in an application to be altered without making substantial "+
                       "changes to the application code. The introduction of support "+
                       "for a pluggable look and feel allowed Swing components to "+
                       "emulate the appearance of native components while still "+
                       "retaining the benefits of platform independence. This feature "+
                       "also makes it easy to have an individual application's appearance "+
                       "look very different from other native programs.\n\n"+
                       "Originally distributed as a separately downloadable library, "+
                       "Swing has been included as part of the Java Standard Edition "+
                       "since release 1.2. The Swing classes are contained in the "+
                       "javax.swing package hierarchy.";
        
        // We create the TextArea and pass the story in as an argument.
        // We also set it to be non-editable, and the line and word wraps set to true.
        JTextArea storyArea = new JTextArea(story);
        storyArea.setEditable(false);
        storyArea.setLineWrap(true);
        storyArea.setWrapStyleWord(true);
        
        // We create the ScrollPane and instantiate it with the TextArea as an argument
        // along with two constants that define the behaviour of the scrollbars.
        JScrollPane area = new JScrollPane(storyArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        // We then set the preferred size of the scrollpane.
        area.setPreferredSize(new Dimension(300, 200));
        
        // and add it to the GUI.
        totalGUI.add(area);
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] Embrace of the JScrollPane [=]");

        ScrollPaneExample demo = new ScrollPaneExample();
        frame.setContentPane(demo.createContentPane());
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(350, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
We start by defining the text that will go in the JTextArea, then instantiate the JTextArea just as we did in the previous tutorial. Editable is off, Word and Line Wrap is on.
34
35
36
37
        JTextArea storyArea = new JTextArea(story);
        storyArea.setEditable(false);
        storyArea.setLineWrap(true);
        storyArea.setWrapStyleWord(true);
Normally, this text would stretch for miles. Using frame.pack(), the JTextArea widget would be sized to fit the text. So, we put the JTextArea widget in a JScrollPane to restrict the size without removing any of the text.

The JScrollPane is instantiated with three arguments. The first is the widget being placed in the JScrollPane, in this case 'storyArea' the JTextArea.
The second and third are constants that define the behaviour of the scrollbars on the scroll pane, vertical followed by horizontal. Here are the selections... The default for this, i.e. if you do not pass these arguments, is to set both to 'as needed'. This in my opinion is the best way to go but it will all depend on what you are trying to do.

For more on JScrollPane Constant Values
41
42
        JScrollPane area = new JScrollPane(storyArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
We need to set the preferred size of the scroll pane, to set how large you want the scroll pane. If/When the widget within the scroll pane becomes larger than this size, the scroll pane will set scrollbars depending on the behaviour you have set.
If you are having problems with the scroll pane re-sizing, set minimum and maximum size like you have done before (see BoxLayout tutorial).
45
        area.setPreferredSize(new Dimension(300, 200));
Once the widget is placed on the scroll pane, we only have to add the scroll pane to the GUI.
48
        totalGUI.add(area);
When it's run, it should look like this.

Picture of a ScrollPane

Questions

Scroll Pane cause you pain? Oh dear :/

Question 1. What is the first argument in the instantiation?

  • a) The Horizontal Scrollbar.
  • b) The widget we place on the scroll pane.
  • c) The Vertical Scrollbar.

Question 2. What is the second argument in the instantiation?

  • a) The Horizontal Scrollbar.
  • b) The widget we place on the scroll pane.
  • c) The Vertical Scrollbar.

Question 3. What is the third argument in the instantiation?

  • a) The Horizontal Scrollbar.
  • b) The widget we place on the scroll pane.
  • c) The Vertical Scrollbar.

Question 4. How do we set the size of the Scroll pane?

  • a) .setPreferredSize(Dimension)
  • b) .setSize(intx, inty)
  • c) .setSize(Dimension)




Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict