Using AdjustmentListener for AWT Scrollbar

The following example illustrates use of AdjustmentEvent with AWT Scrollbar.

import java.awt.*;
import java.awt.event.*;
class ScrollbarEvent extends Frame implements AdjustmentListener
{
Scrollbar s;

    public ScrollbarEvent()
    {
        createAndShowGUI();
    }
  
    private void createAndShowGUI()
    {
        setTitle("Scrollbar with AdjustmentListener Demo");
        setLayout(new FlowLayout());
      
        // Create and add scrollbar
        s=new Scrollbar();
        add(s);
      
        // Make it fat!
        s.setPreferredSize(new Dimension(50,250));
      
        // Add adjustment listener
        s.addAdjustmentListener(this);
      
        setSize(400,400);
        setVisible(true);
    }
  
    // Called whenever the scrollbar value changes
    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        // Update the title
        setTitle("Current value: "+ae.getValue());
    }
  
    public static void main(String args[])
    {
        new ScrollbarEvent();
    }
}

ScrollbarEvent(): Code illustrating AdjustmentListener with AWT Scrollbar is written here.
new ScrollbarEvent(): Create object for the class ScrollbarEvent

Using AdjustmentListener for AWT Scrollbar

Previous: Using WindowFocusListener for AWT Frame

No comments: