Java AWT Scrollbar Insets

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
     }
}

Java AWT Scrollbar Insets
Output screen of Java AWT Scrollbar Insets

5 thoughts on “Java AWT Scrollbar Insets

  1. v.s.

    sir y this program setBounds() method not working means its showing the scrollbar in the whole screen not in the specified place

    import java.awt.*;
    import java.awt.event.*;
    class a extends Frame
    {
    a()
    {

    Scrollbar redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
    redScroll.setUnitIncrement(5);
    redScroll.setBlockIncrement(15);
    add(redScroll);

    redScroll.setBounds(0,878,155,60);

    setSize(300,300);
    setVisible(true);

    }

    public static void main(String []aa)
    {
    new a();
    }

    }

    1. S. Nageswara Rao, Corporate Trainer Post author

      See this code:
      import java.awt.*;
      import java.awt.event.*;
      class a extends Frame
      {
      a()
      {
      setLayout(null);
      Scrollbar redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
      redScroll.setUnitIncrement(5);
      redScroll.setBlockIncrement(15);

      redScroll.setBounds(0,280,300, 20);
      add(redScroll);

      setSize(300,300);
      setVisible(true);

      }

      public static void main(String []aa)
      {
      new a();
      }
      }

      Know the method signature of setBounds() method:

      setBounds(int x, int y, int width, int height);

      1. v. s.

        sir in button creation without using “setLayout();” method we can put our button anywhere in the frame with the help of setBounds() method but here if we remove “setLayout(null);” in the above program then setBounds() method doesnt show the required result it will put the scrollbar in the whole frame and it will show the required result if we put “setLayout(null);” along with “setBounds()” method . why it happens?

Leave a Reply

Your email address will not be published. Required fields are marked *