Java Now

Java AWT Scrollbar Examples

Site Logo

Usage:

The Scrollbar lets the user graphically select a value by sliding a knob within a bounded interval. 

Package:

Java.awt

Subclass of:

Java.awt.Component

Common Constructors:

Scrollbar ()
Creates a Scrollbar instance with a range of 0-100, an initial value of 0, and vertical orientation.
Scrollbar (int Orientation)
Creates a Scrollbar instance with a range of 0-100, an initial value of 0, and the specified orientation.

Common Methods:

addAdjustmentListener (AdjustmentListener Handler)
Configures an event handler for the scrollbar.
getValue ()
Returns the value of the scrollbar setting.
setBackground (Color BackgroundColor)
Sets the background color of the scrollbar.
setMaximum (int Max)
Sets the maximum value of the scrollbar.
setMinimum (int Min)
Sets the minimum value of the scrollbar.
setValue (int Value)
Sets the current value of the scrollbar.

Arguments:

BackgroundColor
The color to be used for the background of the field.
Handler
The object which handles action events from this field.
Max
The maximum value of the scrollbar.
Min
The minimum value of the scrollbar.
Orientation
The orientation of the scrollbar.
Scrollbar.HORIZONTAL
Orients the scrollbar horizontally.
Scrollbar.VERTICAL
Orients the scrollbar vertically.
Value
The current value of the scrollbar.

Example:

Code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Scrollbar2 extends Applet implements AdjustmentListener {
  /* Declaration */
  private LayoutManager Layout;
  private Scrollbar HSelector;
  private Scrollbar VSelector;
  private Drawing Pad;
  private Label Report;

  public Scrollbar2 () {
    /* Instantiation */
    Layout = new BorderLayout ();
    HSelector = new Scrollbar ();
    VSelector = new Scrollbar (Scrollbar.VERTICAL);
    Pad = new Drawing ();
    Report = new Label ();

    /* Decoration */
    HSelector.setMaximum (300);
    HSelector.setOrientation (Scrollbar.HORIZONTAL);
    VSelector.setMaximum (300);
    Report.setAlignment (Label.CENTER);
    Pad.setBackground (Color.yellow);
    
    /* Location */
    setLayout (Layout);
    add ("South", Report);   
    add ("North", HSelector);
    add ("West", VSelector);
    add ("Center", Pad);
                    
    /* Configuration */
    HSelector.addAdjustmentListener (this);
    VSelector.addAdjustmentListener (this);
    
    /* Initialization */
    HSelector.setValue (100);
    VSelector.setValue (150);
    Pad.setOval (100, 150);
    Report.setText ("H = " + HSelector.getValue() +
                    ", V = " + VSelector.getValue());
  }

  public void adjustmentValueChanged(AdjustmentEvent e) {
    Report.setText ("H = " + HSelector.getValue() +
                    ", V = " + VSelector.getValue());
    Pad.setOval (HSelector.getValue (), VSelector.getValue ());
    Pad.repaint();
  } 

}

class Drawing extends Canvas {
  /* Declaration */
  private int X; 
  private int Y;
          
  public void setOval (int H, int V) {
    X = H;
    Y = V;              
  }             
          
  public void paint (Graphics g) {
    g.drawOval (10, 10, X, Y);
  }

}
Java AWT Controls Sampler  Java AWT Controls Sampler
Java Swing JScrollbar Examples  Java Swing JScrollbar Examples
Java Swing JSlider Examples  Java Swing JSlider Examples
Class Scrollbar API  Class Scrollbar API  Sun Java site
Java Now Home  Java Now Home
Java Examples Index  Java Examples Index
Java AWT Examples Index  Java AWT Examples Index
E-Mail
Valid HTML 4.01 Transitional Valid CSS! Site Logo

copyright 2001-2006, j.h.young, revised 2/7/06

Java AWT Controls Sampler / Java Swing JScrollbar Examples / Java Swing JSlider Examples / Class Scrollbar API /