/* * File: ScrollbarTest.java * * Create a scrollbar (i.e., a slide control), with scrollbox, unit * and block increment, and direction. Shows Adjustment event and * getting the new value to display in a label. * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.Label; import java.awt.Scrollbar; import java.awt.Color; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; public class ScrollbarTest extends java.applet.Applet implements AdjustmentListener { private Label labelLeft, labelCenter, labelRight; private Scrollbar scrollbar; public void init() { setBackground( Color.white ); Color lightBlue = new Color( 0xB0, 0xE0, 0xE6 ); int min = 0; // minimum value of scrollbar labelLeft = new Label( String.valueOf( min ) ); labelLeft.setAlignment( Label.LEFT ); labelLeft.setFont( new Font( "Serif", Font.PLAIN, 18 ) ); labelLeft.setForeground (Color.green); int max = 100; // maximum value of scrollbar labelRight = new Label( String.valueOf( max ) ); labelRight.setAlignment( Label.RIGHT ); labelRight.setFont( new Font( "Serif", Font.PLAIN, 18 ) ); labelRight.setForeground (Color.green); int pos = 50; // initial position of scrollbox labelCenter = new Label( String.valueOf( pos ) ); labelCenter.setAlignment( Label.CENTER ); labelCenter.setFont( new Font( "Serif", Font.BOLD, 30 ) ); int orientation = Scrollbar.HORIZONTAL; int size = 5; // relative size of the scrollbox scrollbar = new Scrollbar( orientation, pos, size, min, max ); scrollbar.setBlockIncrement(10); scrollbar.setBackground( lightBlue ); // Add components to the applet: setLayout( new BorderLayout() ); add( labelLeft, BorderLayout.WEST ); add( labelRight, BorderLayout.EAST ); add( labelCenter, BorderLayout.CENTER ); add( scrollbar, BorderLayout.SOUTH ); // Listen for AdjustmentEvents: scrollbar.addAdjustmentListener( this ); } public void adjustmentValueChanged( AdjustmentEvent event ) { int v = event.getValue(); labelCenter.setText( String.valueOf(v) ); } }