Way2Java

Java AWT Scrollbar Insets

The scrollbar scrolls through a range of integer values. It is a very convenient component to take a value from the user within a range of values. Scrollbar have two orientations: Vertical or Horizontal. Scrollbar can be set with maximum and minimum values within which the scrollbar moves. The scroll bar may be given an initial value. At this value, the scroll box is positioned by default in the scroll bar.

Event handler for Scrollbar

Scrollbar generates AdjustmentEvent and is handled by AdjustmentListener. The AdjustmentListener includes only one abstract method adjustmentValueChanged() which must be overridden with event handling code.

Scrollbar Basics

Scrollbar is a well known window gadget in Windows environment. Scrollbar comes with some properties mentioned herewith.

Property Description
Alignment(orientation) A scrollbar can be either
vertical
or horizontal. The default is vertical.
Unit increment When the user clicks the arrow on either end of the scrollbar, the scrollbar value is changed by this amount for which it is set. By default is it is 1. The value can be set explicitly with the method setUnitIncrement().
Block increment When the user clicks anywhere in the area between scroll box (also known as thumb) and arrow, the value is changed by this amount. By default is it is 10. The value can be set explicitly with the method setBlockIncrement().
Minimum and Maximum values It is the range of values within which the scroll box moves. Beyond these values, the scroll box cannot move.
Initial value It is the value where by default the scroll box is positioned. Or to say, it is the default value given by the scrollbar when displayed.
Scroll box size This gives the size of the scroll box. It is actually the maximum value minus the visible amount. Visible amount can be set explicitly. For example, if the maximum value is 300 and the visible amount is 50, the size of the scroll box is 250 (300 – 50). But the scroll box can scroll to a maximum value of 300.

Following is class signature of java.awt.Scrollbar

public class Scrollbar extends Component implements Adjustable, Accessible

Following program illustrates three horizontal scrollbars which moves between the values 0 to 255. The values of these scrollbars are taken as RGB values, constructed a Color object and set to the frame. As the scrollbar moves the background of the frame changes.

Example on Java AWT Scrollbar Insets

import java.awt.*;
import java.awt.event.*;
public class ScrollDemo extends Frame implements AdjustmentListener
{                   
    Scrollbar redScroll, greenScroll, blueScroll;                            
    Label redLabel, greenLabel, blueLabel;  
    Panel p1;

    public ScrollDemo()
    {
          setBackground(Color.yellow);  
          p1 = new Panel();
          p1.setLayout(new GridLayout(3, 2, 5, 5));  

          redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0,  0, 255);
          redScroll.setUnitIncrement(5);                           //default is 1
          redScroll.setBlockIncrement(15);                         //default is 10
          p1.add(redLabel = new Label("RED"));  
          p1.add(redScroll);
                                                                   // similarly set for green scroll bar
          greenScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
          greenScroll.setUnitIncrement(5);
          greenScroll.setBlockIncrement(15);
          p1.add(greenLabel = new Label("GREEN"));
          p1.add(greenScroll);

          blueScroll = new Scrollbar();
          blueScroll.setOrientation(Scrollbar.HORIZONTAL);  
          blueScroll.setValue(0);
          blueScroll.setVisibleAmount(0);
          blueScroll.setUnitIncrement(5);  
          blueScroll.setBlockIncrement(10);
          blueScroll.setMinimum(0);
          blueScroll.setMaximum(255);
          p1.add(blueLabel = new Label("BLUE"));
          p1.add(blueScroll);

          redScroll.addAdjustmentListener(this);
          greenScroll.addAdjustmentListener(this);
          blueScroll.addAdjustmentListener(this);

          add(p1,"South");

          setTitle("Playing With Colors");
          setSize(450,325);
          setVisible(true);
      }

      public Insets getInsets()  
      {
         Insets is1 = new Insets(5, 8, 10, 25);
         return is1;
      }
       
      public void adjustmentValueChanged(AdjustmentEvent e)
      {
         int rv = redScroll.getValue();  
         int gv = greenScroll.getValue();
         int bv = blueScroll.getValue();

        redLabel.setText("RED: "+ rv);
        greenLabel.setText("GREEN: "+ gv);
        blueLabel.setText("BLUE: "+ bv);

       Color clr1 = new Color(rv, gv, bv);
       setBackground(clr1);
     } 
     public static void main(String args[])
     {
         new ScrollDemo();
     }
}


Output screen of Java AWT Scrollbar Insets


Java AWT Scrollbar Insets

Three scrollbar objects redScroll, greenScroll, blueScroll and three labels redLabel, greenLabel, blueLabel are created.

p1.setLayout(new GridLayout(3, 2, 5, 5));

Panel p1 is created and layout is set to GridLayout with 3 rows and 2 columns to accommodate three scrollbars and three labels. Horizontal and vertical gaps are given each with 5 pixels.

redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);

redScroll scrollbar is created and properties are set through the constructor.

Scrollbar.HORIZONTAL: alignment
First 0: It is for initial value where to place the scroll box
Second 0: It is the scroll box size; 0 gives minimum size
Third 0: Gives minimum value
255: Gives the maximum value

The scrollbar moves in between the values 0 to 255.

redScroll.setUnitIncrement(5); //default is 1
redScroll.setBlockIncrement(15); //default is 10
p1.add(redLabel = new Label(“RED”));
p1.add(redScroll);

setUnitIncrement(5): Sets the value to be jumped when clicked on the arrows of the scroll bar. It is set to 5 and if not set, the default is 1.

setBlockIncrement(15): Sets the value to be jumped when clicked in the space between the arrow and scroll box. It is set to 15 and if not set, the default is 10.

The third scroll bar blueScroll is created in a different way. The default constructor is used to create the object and the values are set later using Scrollbar methods.

blueScroll = new Scrollbar();
blueScroll.setOrientation(Scrollbar.HORIZONTAL);
blueScroll.setValue(0);
blueScroll.setVisibleAmount(0);
blueScroll.setMinimum(0);
blueScroll.setMaximum(255);

The setValue() gives the position of the scroll box on the scrollbar when the frame is created. The setVisibleAmount() gives the scroll box size and 0 gives the minimum size. setMinimum() and setMaximum() methods set the minimum and maximum values in between which the scroll box moves; other way, they give the scrollable values of the scroll bar).

Insets is1 = new Insets(5, 8, 10, 25);

getInsets() is the method of Frame which return an object of Insets class. Insets class gives the margin between the border of the frame and the components inside it. An object of Insets is created with top gap as 5, left gap as 8, bottom gap as 10 and right gap as 25. The Insets object is1 is returned which sets the gap.

int rv = redScroll.getValue();
redLabel.setText(“RED: ” + rv);

The getValue() method of Scrollbar returns the scroll box position on the scroll bar; other way, the scrollbar value. This value rv is displayed as a label redLabel with setText() method.

A Color object clr1 is created by taking the latest values of the three scrollbars and applied to frame with setBackground(clr1) method. Now, as the scrollbars are disturbed the color of the frame changes.

Note: For simplicity, the window closing code is not included. For closing the frame, 4 styles exist and you can apply any one to this program to close the frame. The styles are discussed in " Java Frame Closing – WindowListener".