AWT Scrollbar Class

Scrollbar control represents a scroll bar component in order to enable user to select from range of values.
Following is the declaration for java.awt.Scrollbar class:
public class Scrollbar
   extends Component
      implements Adjustable, Accessible
Following are the fields for java.awt.Image class:
static int HORIZONTAL  --A constant that indicates a horizontal scroll bar.
static int HORIZONTAL  --A constant that indicates a horizontal scroll bar.
static int VERTICAL  --A constant that indicates a vertical scroll bar.
static int VERTICAL  --A constant that indicates a vertical scroll bar.
Scrollbar() 
Constructs a new vertical scroll bar.
Scrollbar(int orientation) 
Constructs a new scroll bar with the specified orientation.
Scrollbar(int orientation, int value, int visible, int minimum, int maximum) 
Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.
void addAdjustmentListener(AdjustmentListener l) 
Adds the specified adjustment listener to receive instances of AdjustmentEvent from this scroll bar.
void addNotify() 
Creates the Scrollbar's peer.
int getBlockIncrement() 
Gets the block increment of this scroll bar.
int getLineIncrement() 
 Deprecated. As of JDK version 1.1, replaced by getUnitIncrement().
int getMaximum() 
Gets the maximum value of this scroll bar.
int getMinimum() 
Gets the minimum value of this scroll bar.
int getOrientation()
 Returns the orientation of this scroll bar.
int getPageIncrement() 
Deprecated. As of JDK version 1.1, replaced by getBlockIncrement().
int getUnitIncrement()
Gets the unit increment for this scrollbar.
int getValue() 
Gets the current value of this scroll bar.
boolean
getValueIsAdjusting() 
Returns true if the value is in the process of changing as a result of actions being taken by the user.
int getVisible() 
Deprecated. As of JDK version 1.1, replaced by getVisibleAmount().
int getVisibleAmount() 
Gets the visible amount of this scroll bar.
protected String paramString() 
Returns a string representing the state of this Scrollbar.
protected void processAdjustmentEvent(AdjustmentEvent e) 
Processes adjustment events occurring on this scrollbar by dispatching them to any registered AdjustmentListener objects.
protected void processEvent(AWTEvent e) 
Processes events on this scroll bar.
void removeAdjustmentListener(AdjustmentListener l) 
Removes the specified adjustment listener so that it no longer receives instances of AdjustmentEvent from this scroll bar.
void setBlockIncrement(int v) 
Sets the block increment for this scroll bar.
void setLineIncrement(int v) 
Deprecated. As of JDK version 1.1, replaced by setUnitIncrement(int).
void setMaximum(int newMaximum) 
Sets the maximum value of this scroll bar.
void setMinimum(int newMinimum) 
Sets the minimum value of this scroll bar.
void setOrientation(int orientation) 
Sets the orientation for this scroll bar.
void setPageIncrement(int v) 
Deprecated. As of JDK version 1.1, replaced by setBlockIncrement().
void setUnitIncrement(int v) 
Sets the unit increment for this scroll bar.
void setValue(int newValue) 
Sets the value of this scroll bar to the specified value.
void setValueIsAdjusting(boolean b) 
Sets the valueIsAdjusting property.
void setValues(int value, int visible, int minimum, int maximum) 
Sets the values of four properties for this scroll bar: value, visibleAmount, minimum, and maximum.
void setVisibleAmount(int newAmount) 
Sets the visible amount of this scroll bar.
AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this Scrollbar.
AdjustmentListener[] getAdjustmentListeners() 
Returns an array of all the adjustment listeners registered on this scrollbar.
<T extends EventListener>T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this Scrollbar.
This class inherits methods from the following classes:
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showScrollbarDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

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

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

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

      final Scrollbar horizontalScroller = new Scrollbar(Scrollbar.HORIZONTAL);
      final Scrollbar verticalScroller = new Scrollbar();
      verticalScroller.setOrientation(Scrollbar.VERTICAL);
      horizontalScroller.setMaximum (100);
      horizontalScroller.setMinimum (1);
      verticalScroller.setMaximum (100);
      verticalScroller.setMinimum (1);

      horizontalScroller.addAdjustmentListener(new AdjustmentListener() {

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

      verticalScroller.addAdjustmentListener(new AdjustmentListener() {

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

      controlPanel.add(horizontalScroller);
      controlPanel.add(verticalScroller);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page