Swing JScrollBar Class



The class JScrollBar is an implementation of scrollbar.

Class declaration

Following is the declaration for javax.swing.JScrollBar class:

public class JScrollBar
   extends JComponent
      implements Adjustable, Accessible


Following are the fields for javax.swing.ImageIcon class:

  • protected int blockIncrement

  • protected BoundedRangeModel model -- The model that represents the scrollbar's minimum, maximum, extent (aka "visibleAmount") and current value.

  • protected int orientation

  • protected int unitIncrement

Class constructors

S.N.Constructor & Description
Creates a vertical scrollbar with the initial values.
2JScrollBar(int orientation)
Creates a scrollbar with the specified orientation and the initial values.
3JScrollBar(int orientation, int value, int extent, int min, int max)
Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum.

Class methods

S.N.Method & Description
1void addAdjustmentListener(AdjustmentListener l)
Adds an AdjustmentListener.
2protected void fireAdjustmentValueChanged(int id, int type, int value)
Notify listeners that the scrollbar's model has changed.
3AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JScrollBar.
4AdjustmentListener[] getAdjustmentListeners()
Returns an array of all the AdjustmentListeners added to this JScrollBar with addAdjustmentListener().
5int getBlockIncrement()
For backwards compatibility with java.awt.Scrollbar.
6int getBlockIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a block (usually "page") up/down request.
7int getMaximum()
The maximum value of the scrollbar is maximum - extent.
8Dimension getMaximumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.
9int getMinimum()
Returns the minimum value supported by the scrollbar (usually zero).
10Dimension getMinimumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.
11BoundedRangeModel getModel()
Returns data model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.
12int getOrientation()
Returns the component's orientation (horizontal or vertical).
13ScrollBarUI getUI()
Returns the delegate that implements the look and feel for this component.
14String getUIClassID()
Returns the name of the LookAndFeel class for this component.
15int getUnitIncrement()
For backwards compatibility with java.awt.Scrollbar.
16int getUnitIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a unit up/down request.
17int getValue()
Returns the scrollbar's value.
18boolean getValueIsAdjusting()
True if the scrollbar knob is being dragged.
19int getVisibleAmount()
Returns the scrollbar's extent, aka its "visibleAmount".
20protected String paramString()
Returns a string representation of this JScrollBar.
21void removeAdjustmentListener(AdjustmentListener l)
Removes an AdjustmentEvent listener.
22void setBlockIncrement(int blockIncrement)
Sets the blockIncrement property.
23void setEnabled(boolean x)
Enables the component so that the knob position can be changed.
24void setMaximum(int maximum)
Sets the model's maximum property.
25void setMinimum(int minimum)
Sets the model's minimum property.
26void setModel(BoundedRangeModel newModel)
Sets the model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.
27void setOrientation(int orientation)
Set the scrollbar's orientation to either VERTICAL or HORIZONTAL.
28void setUI(ScrollBarUI ui)
Sets the L&F object that renders this component.
29void setUnitIncrement(int unitIncrement)
Sets the unitIncrement property.
30void setValue(int value)
Sets the scrollbar's value.
31void setValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property.
32void setValues(int newValue, int newExtent, int newMin, int newMax)
Sets the four BoundedRangeModel properties after forcing the arguments to obey the usual constraints.
33void setVisibleAmount(int extent)
Set the model's extent property.
34void updateUI()
Overrides JComponent.updateUI.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

JScrollBar Example

Create the following java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >

package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    


      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());


  private void showScrollbarDemo(){                                       
      headerLabel.setText("Control in action: JScrollbar"); 

      final JScrollBar horizontalScroller = 
         new JScrollBar(JScrollBar.HORIZONTAL);
      final JScrollBar verticalScroller = new JScrollBar();
      horizontalScroller.setMaximum (100);
      horizontalScroller.setMinimum (1);
      verticalScroller.setMaximum (100);
      verticalScroller.setMinimum (1);

      horizontalScroller.addAdjustmentListener(new AdjustmentListener() {

         public void adjustmentValueChanged(AdjustmentEvent e) {
            statusLabel.setText("Horozontal: "
               +" ,Vertical: "
               + verticalScroller.getValue());

      verticalScroller.addAdjustmentListener(new AdjustmentListener() {

            public void adjustmentValueChanged(AdjustmentEvent e) {
               statusLabel.setText("Horozontal: "
               +" ,Vertical: "+ verticalScroller.getValue());



Compile the program using command prompt. Go to D:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output

Swing JScrollbar