In many cases, components of our program interfaces will be too large to display on the screen. This happens most commonly with text fields, which may need to display large sets of text in a limited space. In these situations, we may want to add scroll bars to our components, to allow a user to move the component view to better view content on the screen.

Basics of Scroll Bars

We can add scroll bars to a variety of different Swing components, including JTextAreaJPanel, and JFrame. To understand how scroll bars work, we can consider an example of a JTextArea drawn on the screen. When the JTextArea is drawn on the screen, it is drawn within the bounds provided. Although the size displayed is limited, the user is able to write text beyond what is visible on the GUI. When text is written beyond what is visible, the text is still there, but it isn’t being displayed, since it is technically off of the interface.

We will often say that data on our GUI is displayed using a view port. The view port is the portion of the GUI that is currently visible to the user on the screen. When our view port is rendered, it will render an area of the possible GUI screen that is the same size or smaller than the targetted GUI screen. This is why a user can write beyond what is visible in the JTextArea, but not always see those changes.

If we want the user to be able to see the whole GUI, we need to give them a method to change the current view port. This is where scroll bars come in. When we add scroll bars onto a component of our GUI, the user is able to move the view port vertically or horizontally. This allows for a more dynamic display for the user.

One way that we can add scroll bars to our GUI is using a JScrollBar. A JScrollBar can be added to any JFrame or JPanel, and requires a special type of listener known as an AdjustmentListener to function. We can attach the listener to the JScrollBar, and it will take action when the scroll bar is changed by the user. The example code below shows a simple implementation where we print the JScrollBar position to the screen on each change.

public class Main {
    public static void main(String[] args) {
        JLabel label = new JLabel();
        JFrame frame = new JFrame("Scroll Bars");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        JScrollBar horizontal = new JScrollBar(JScrollBar.HORIZONTAL, 0, 100, 0, 500);
        horizontal.addAdjustmentListener(new MyAdjustmentListener(label, frame));
        frame.getContentPane().add(horizontal, BorderLayout.SOUTH);
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

class MyAdjustmentListener implements AdjustmentListener {
    private final JLabel label;
    private final JFrame frame;

    public MyAdjustmentListener(JLabel label, JFrame frame) {
        this.label = label;
        this.frame = frame;
    }

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        label.setText("Slider's position is " + e.getValue());
        frame.repaint();
    }
}

When we declare a JScrollBar, we provide 5 arguments to the object. The first tells us if the scroll bar will have a horizontal or vertical orientation. The second argument is the initial value of the scroll bar. The value 0 would indicate that the scroll bar starts at the furthest left location for horizontal, or closest to the top if it is vertical. The third argument is the extent, which indicates how large the scroll bar should be. The fourth argument is the minimum value for the scroll bar, and the final argument is the maximum value for the scroll bar.

One of the main uses of a JScrollBar would be to implement a GUI with multiple pages. A user can click the scroll bar, and on the AdjustmentListener, we can adjust the view to display content for the next page. If we want to implement a more traditional scroll bar that gradually scrolls to show more content, we will need to use a scroll pane.

Scroll Panes

The JScrollPane allows us to make a GUI component scrollable. The JScrollPane works similar to a JFrame, in the sense that we can add components into it. When a component is added into a JScrollPane, it is able to be scrolled to adjust what content is shown in the view port. In the example below, we create a JScrollPane, which contains a JTextArea.

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Scroll Bars");
        JTextArea textArea = new JTextArea(5, 5);
        JScrollPane scrollPane = new JScrollPane(textArea);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        frame.getContentPane().add(scrollPane);
        frame.setVisible(true);
    }
}

When the code is run, the JTextArea will at first appear normal. When the user writes beyond the viewable portion of the JTextArea, the scroll bars will automatically appear, allowing the user to scroll the view port to view any section of text. One note about JScrollPane is that it is a single child container. This means that you can only place one component inside of a JScrollPane. Therefore, if you need multiple scrollable components, you need multiple JScrollPane instances.

Scroll Bar Policies

When you add a component into a JScrollPane, you will find that it is able to always scroll vertically and horizontally, depending on what is required. There are situations where we may want control over which scroll bars are available for a component. For example, we may want a text area that can scroll vertically as required, but not horizontally. In this situation, we need to set a scroll bar policy for our JScrollPane.

To do this, we use two methods, called setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy. There are six possible policies we can set for our scroll bars:

  1. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS: The horizontal scroll bar is always displayed
  2. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER: The horizontal scroll bar is never displayed
  3. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED: The horizontal scroll bar is displayed when it is required
  4. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS: The vertical scroll bar is always displayed
  5. JScrollPane.VERTICAL_SCROLLBAR_NEVER: The vertical scroll bar is never displayed
  6. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED: The vertical scroll bar is displayed when it is required

The example below shows how we can set the scroll bar policy for a given JScrollPane.

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Scroll Bars");
        JTextArea textArea = new JTextArea(5, 5);

        JScrollPane scrollPane = new JScrollPane(textArea);

        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        frame.getContentPane().add(scrollPane);
        frame.setVisible(true);
    }
}

We have now explored how we can create scroll bars in our applications, as well as customize when they display. With this, you can now design dynamic components, as well as user-friendly text inputs.

Leave a Reply

Your email address will not be published.