Turn Desktop View Off
by Dinesh Thakur

A scrollbar is a component that allows a user to select any numeric value by dragging a slider within a range defined by a minimum and maximum. A scrollbar may be oriented horizontally or vertically. A scrollbar consists of several individual parts: arrows (the buttons at each end of the scrollbar), a slider box or thumb (scrollable box you slide) and a track (part of the scrollbar you slide the thumb in).

Whenever the user clicks the arrows on either end, the current value of the scrollbar is modified by a unit depending upon direction of the arrows. The slider box indicates the current value of the scrollbar. The user can also click anywhere on the track to move the slider box to jump in that direction by some value larger than 1. It can be formed with the following constructors:

Scrollbar();
Scrollbar(int style);
Scrollbar(int style. int initialvalue, int thumbsize,int min. int max);
The second constructor creates a scroll bar with specified orientation, that is, horizontal or vertical. The third form specifies the initial value and the maximal value along with the thumb size. After initiating the Scroll with the default constructor, the values can be set using the following method:
void setValues(int initial. int thumbsize. int max);
The methods getValue() and setValue() are used to get and set the value of the scroll bar at a particular instant. The unit increment and block increment can be set using the methods:
void setUnitlncrement(int new)
void setBlocklncrement(int new)
The minimum value and the maximum value of the scrollbar can be obtained using getMinimum( ) and getMaximum( ) methods. There are two types of the scrollbarsvertical and horizontal. When creating scrollbars, the constants Scrollbar.VERTICAL and Scrollbar.HORIZONTAL are used. When a block in the scrollbar is adjusted, AdjustmentEvent is generated and hence the AdjustmentListener interface has to be used to handle the scrollbar. Program illustrates the use of scroll bars .

Scrollbar Interface

Method

Returns

Notes

Scrollbar(),

 

Orientation is either horizontal or vertical; value is starting value; visible is the page size; minimum and maximum are the reporting range for the ends of the bar

Scrollbar(int orientation),

 

 

Scrollbar(int orientation, int value, int visible, int minimum, int maximum),

 

 

getLineIncrement()

Int

How much is each “click” worth?

getMaximum()

Int

What is the largest value the scrollbar will report?

getMiniimum()

Int

What is the smallest value the scrollbar will report?

getOrientation()

Int

Either horizontal or vertical

getPageIncrement()

Int

How much is a page movement worth? 

getValue()

Int

What is the current value of the bar?

getVisible()

Int

How big is the handle(in line increments)

setLineIncrement(int value)

Void

Each click of the up/left, right/down buttons moves the current value this far

setPageIncrement(int value)

Void

Each click of the page up/left, right/down moves the current value this far

setValue(int value)

Void

Set the handle at this value

setValues(int value, int visible, int minimum, int maximum)

Void

Reset the scrollbar to reflect the bounds passed in

 

import java.awt.*;

class ScrollBarExample extends Frame
{
     ScrollBarExample()
     {
           setLayout(new FlowLayout());
           //Horizontal Scrollbar with min value 0,max value 100,initial value 50 and visible amount 10
           Label lblHor =new Label("Horizontal Scrollbar");
           Scrollbar sl = new Scrollbar(Scrollbar.HORIZONTAL,50,10,0,100);
           //Vertical Scrollbar with min value 0,max value 255,initial value 10 and visible amount 5
           Label lblver =new Label("Vertical Scrollbar");
           Scrollbar s2 = new Scrollbar(Scrollbar.VERTICAL,10,5,0,10);
           add(lblHor); add(sl);
           add(lblver); add(s2);
     }
}
     class ScrollBarJavaExample
     {
           public static void main(String args[])
          {
               ScrollBarExample frame = new ScrollBarExample();
               frame.setTitle("Scrollbar in Java Example");
               frame.setSize(520,200);
               frame.setVisible(true);
          }
     }

Scrollbar