Java Swing Tutorial - Java Swing JScrollBar








A JScrollBar has an orientation property that determines whether it is displayed horizontally or vertically.

A JScrollBar is made up of four parts: two arrow buttons, a thumb, and a track.

When the arrow button is clicked, the knob moves on the track towards the arrow button.

We can drag the thumb towards either end with the help of a mouse. To move the thumb, click on the track.

The following table lists commonly used properties of a JScrollBar and Methods to Get/Set Those Properties.

IDMethod/Description
1getOrientation()
setOrientation()
Determines whether the JScrollBar is horizontal or vertical. Its value could be one of the two constants, HORIZONTAL or VERTICAL, which are defined in the JScrollBar class.
2getValue()
setValue()
The position of the knob is its value. Initially, it is set to zero.
3getVisibleAmount()
setVisibleAmount()
It is the size of the knob. It is expressed in proportion to the size of the track.
4getMinimum()
setMinimum()
The minimum value that it represents. The default value is zero.
5getMaximum()
setMaximum()
The maximum value that it represents. The default value is 100.

The following code demonstrates how to create a JScrollBar with different properties.

To create a JScrollBar with all default properties. Its orientation will be vertical, current value 0, extent 10, minimum 0, and maximum 100.

JScrollBar sb1  = new JScrollBar();

To create a horizontal JScrollBar with default values

JScrollBar sb2  = new JScrollBar(JScrollBar.HORIZONTAL);

To create a horizontal JScrollBar with a current value of 50, extent 15, minimum 1 and maximum 150.

JScrollBar sb3  = new JScrollBar(JScrollBar.HORIZONTAL, 50,   15,   1, 150);

The current value of a JScrollBar can be set only between its minimum and (maximum - extent) value.

To handle an AdjustmentListener from a JScrollBar, add an AdjustmentListener to a JScrollBar named myScrollBar.

myScrollBar.addAdjustmentListener((AdjustmentEvent e)  ->  {
    if (!e.getValueIsAdjusting())  {
        // The logic for value changed  goes  here
    }
});




JTextField with a JScrollBar for Scrolling

The following code shows how to use BoundedRangeModel to link JTextField and JScrollBar.

import java.awt.BorderLayout;
/*from  w  w  w  .j ava2 s .  co  m*/
import javax.swing.BoundedRangeModel;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextField textField = new JTextField();

    JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    BoundedRangeModel brm = textField.getHorizontalVisibility();
    scrollBar.setModel(brm);
    panel.add(textField);
    panel.add(scrollBar);


    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}