Java Tutorial

AWT Scrollbar

Scrollbar is the GUI component which allows us to see invisible number of rows and invisible number of columns with vertical and horizontal scrollbars.

Write a java program which illustrates the concept of Scrollbar?

Answer:

import java.awt.*; 
import java.awt.event.*; 
import java.applet.*;
/*<applet code="Scroll" width=300 height=300> </applet>*/
public class Scroll extends Applet implements AdjustmentListener
{
    Scrollbar hsb, vsb; 
    int hr, vr;
    public void init ()
    {
        hsb=new Scrollbar (Scrollbar.HORIZONTAL, 10, 100, 10, 1000);
        vsb=new Scrollbar (Scrollbar.VERTICAL, 10, 100, 10, 1000); 
        add (hsb);
        add (vsb); 
        hsb.addAdjustmentListener (this); 
        vsb.addAdjustmentListener (this); 
        setVisible (true);
    }
    public void adjustmentValueChanged (AdjustmentEvent ae)
    {
        hr=hsb.getValue (); 
        vr=vsb.getValue (); 
        repaint ();
    }
    public void paint (Graphics g)
    {
        g.setColor (Color.cyan); 
        g.fillOval (20, 20, hr, vr);
    }
};