/* * 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.*; import java.awt.event.*; import javax.swing.*; public class ScrollbarTest extends JApplet implements AdjustmentListener { private JLabel labelLeft, labelCenter, labelRight; private JScrollBar scrollbar; public void init() { getContentPane().setBackground( Color.white ); Color lightBlue = new Color( 0xB0, 0xE0, 0xE6 ); int min = 0; // minimum value of scrollbar labelLeft = new JLabel( String.valueOf( min ),JLabel.LEFT ); labelLeft.setFont( new Font( "Serif", Font.PLAIN, 18 ) ); labelLeft.setForeground (Color.green); int max = 105; // maximum value of scrollbar labelRight = new JLabel( String.valueOf( max-5 ), JLabel.RIGHT ); labelRight.setFont( new Font( "Serif", Font.PLAIN, 18 ) ); labelRight.setForeground (Color.green); int pos = 50; // initial position of scrollbox labelCenter = new JLabel( String.valueOf( pos ), JLabel.CENTER ); labelCenter.setFont( new Font( "Serif", Font.BOLD, 30 ) ); int orientation = JScrollBar.HORIZONTAL; int size = 5; // relative size of the scrollbox scrollbar = new JScrollBar( orientation, pos, size, min, max ); scrollbar.setBlockIncrement(10); scrollbar.setBackground( lightBlue ); // Add components to the applet: getContentPane().setLayout( new BorderLayout() ); getContentPane().add( labelLeft, BorderLayout.WEST ); getContentPane().add( labelRight, BorderLayout.EAST ); getContentPane().add( labelCenter, BorderLayout.CENTER ); getContentPane().add( scrollbar, BorderLayout.SOUTH ); // Listen for AdjustmentEvents: scrollbar.addAdjustmentListener( this ); } public void adjustmentValueChanged( AdjustmentEvent event ) { int v = event.getValue(); labelCenter.setText( String.valueOf(v) ); } }