< Prev
Next >

AdjustmentEvent and AdjustmentListener

An event of type AdjustmentEvent is generated when a scrollbar is dragged around.

Some methods of AdjustmentEvent class

Methods Description
public int getValue() Gets the Scrollbar's current position value.
public int getAdjustmentType() Returns the type of adjustment that caused AdjustmentEvent.

A class to listen and respond to AdjustmentEvent, must perform the two steps:

Methods Description
public void addAdjustmentListener(AdjustmentListener object) where object is an object of the class that has implemented AgjustmentListener interface. Doing this, registers the class to listen and respond to mouse move and drag AdjustmentEvent.

Handling an AdjustmentEvent by implementing AdjustmentListener interface

In this upcoming code, we are going to listen to the movement of scrollbar which triggers AdjustmentEvent, by implementing the AdjustmentListener interface.

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

public class ScrollEx2 implements AdjustmentListener
Frame jf;
Panel jp, jp2; 
Label frameLabel1;

jf = new JFrame("Scrollbar");

//Creating the first JPanel and adding two JLabels to it
jp = new Panel();

//Creating a Label 
Label panelLabel1 = new Label("Handling a Scrollbar drag event", Label.CENTER);

jp = new Panel(new BorderLayout());

//Adding the Label to NORTH of the Panel

//Creating the horizontal Scrollbar
Scrollbar scrollBHorizontal = new Scrollbar(Scrollbar.HORIZONTAL, 10, 40, 0, 100);

//Creating the vertical Scrollbar
Scrollbar scrollBVertical = new Scrollbar(Scrollbar.VERTICAL, 10, 60, 0, 100); 

//Adding the horizontal Scrollbar to SOUTH of Panel

//Adding the vetical Scrollbar to EAST of JPanel
jp.add(scrollBVertical, BorderLayout.EAST);

//Getting the current position value of horizontal scrollbar
Integer i = scrollBHorizontal.getValue();

//Creating a JLabel and setting its value to the current position value of horizontal scrollbar.
frameLabel1 = new Label(i.toString());

//Adding this JLabel to SOUTH of the JFrame
jf.add(frameLabel1, BorderLayout.SOUTH);

//Adding the first JPanel to the CENTER of JFrame

//Registering class ScrollEx2 to catch and respond to scrollbar adjustment events 


public void adjustmentValueChanged(AdjustmentEvent e)
Integer i =e.getValue();

public static void main(String... ar)
new ScrollEx2();

When you run the code, you are presented a window that shows an image, with two Scrollbar, i.e. a horizontal and a vertical scrollbar beneath the horizontal Scrollbar, you are shown the number that shows the current position value of Scrollbar.

Figure 1

When you drag any of the scrollbar(horizontal or vertical), you may see the change in the current position value of the scrollbar which you are dragging.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< WindowListener
TextListener >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next