5.2.4 The Scrollbar Class


The following example is an applet that contains a scrollbar (or slider) and a canvas, i.e., a drawing area. The scrollbar points at values for gray shadings of the drawing area that range from black to white.

The Applet
The Java Code
import java.applet.Applet;
import java.awt.*;

public class GraySlider extends Applet {

     private Scrollbar slider;
     private Canvas canvas;

     public void init() {
       //  create the horizontal scrollbar and the canvas
       slider = new Scrollbar(Scrollbar.VERTICAL, 0, 51, 0, 255);
       //  start with black canvas
       canvas = new Canvas();
       canvas.setBackground(new Color(0,0,0));
       //  choose border layout and add components
       setLayout(new BorderLayout());
       add("West", slider);
       add("Center", canvas);
    }

    public boolean handleEvent(Event evt) {
      if (evt.target == slider) {    // slider moved?
         int gl = slider.getValue(); // get graylevel
         canvas.setBackground(new Color(gl,gl,gl)); // change background color
      }
      return true;
    }
}

The way we have created the scrollbar needs some explanation. A vertical scrollbar looks as follows:

A scrollbar allows the user to select an integer value from a range of values. The position of the tab specifies the currently selected value. The user can move the scrollbar in three ways:
  1. dragging the tab
  2. clicking in the page-up or page-down region
  3. clicking on the up- or down-arrow
The most important instance variables of a Scrollbar are: You can specify one or more of these values when the scrollbar is created. In our applet,

slider = new Scrollbar(Scrollbar.VERTICAL, 0, 51, 0, 255);

sets up a vertical scrollbar, with initial value 0, with a tab of size 51, that increments or decrements by 51 when you click in the paging area of the scrollbar, and that has minimum value of 0 and maximum value of 255.

The current value of the scrollbar is used to define a gray color for the drawing area: the red, green and blue components in the RGB-color scheme are given the the current value of the scrollbar between 0 and 255 (8-bit color specification), where 0 means black, and 255 means white.

The current value of the scrollbar is obtained via the getValue() method. Of course the opposite can be done as well: the setValue(int value) method sets the value of the scrollbar to the specified one. The following table summarizes all constructors and main methods for scrollbars.

Constructors Meaning
Scrollbar() creates a vertical scrollbar
Scrollbar(int orientation) creates a scrollbar with specified orientation; available orientations are Scrollbar.HORIZONTAL (0) and Scrollbar.VERTICAL (1)
Scrollbar(int orientation, int value, int sVisible, int minimum, int maximum) creates a scrollbar with the specified orientation, initial value, tab size, and minimum and maximum values
Methods Meaning
getLineIncrement() returns the line increment (default: 1)
getMaximum() returns the maximum value
getMinimum() returns the minimum value
getOrientation() returns the orientation (default: 1, vertical)
getPageIncrement() returns the page increment (default: 10)
getValue() returns the current value
getVisible() returns the size of the tab
setLineIncrement(int lineIncrement) changes the line increment
setMaximum(int maximum) changes the maximum value
setMinimum(int minimum) changes the minimum value
setOrientation(int orientation) sets the orientation
setPageIncrement(int lineIncrement) changes the page increment
setValue(int value) changes the current value
setValues(int value, int sVisible, int minimum, int maximum) changes the values (value, sVisible, minimum, maximum) that define the scrollbar
setVisible(int sVisible) changes the size of the tab