Adjustment Listener

                                 
         


                                  Adjustment listener in Java

Hello all!๐Ÿ™‹ Here we are going to study about Adjustment listener in java. (To learn about Action listener see the previous post.) Lets begin...


As the name suggests that this listener will listen the adjustment means it is used to receive adjustment events. The important thing to be noticed is that we cannot use this listener without importing java.awt.event.AdjustmentListener or java.awt.event.*  by both the ways we can import the adjustment listener files.

The class in which we want to use adjustment listener needs to implements this interface by the following way:

public class classname implements AdjustmentListener

{

//code

}

Adjustment listener class declaration:-

Following is the declaration for java.awt.event.AdjustmentListener interface:

public interface AdjustmentListener extends EventListener

( you can check this under AdjustementListener interface this class declaration will be shown there)


Interface Method Summary

Modifier and Type
Method
Description
Void
adjustmentValueChanged
Invoked when the value of the adjustable has changed.



Method used to add Adjustment listener

Modifier and Type
Method
Description
void
addAdjustmentListener(AdjustmentListener object)
where object is an object of the class that has implemented AdjustmentListener interface. Doing this, registers the class to listen and respond to AdjustmentEvent.



//Program to illustrate the use of adjustment listener in java.


import java.awt.*;

import java.awt.event.*;

public class adjustmentlistener extends Frame implements AdjustmentListener {

       Scrollbar s;

        adjustmentlistener()

        {

           createAndShowGUI();

        }

        private void createAndShowGUI()

        {

          setTitle("Scollbar_Adjustment_Listener");//title of frame

          setLayout(new FlowLayout());//layout of frame

          //create and add scrollbar

          s= new Scrollbar();

          add(s);

          //size of scrollbar

          s.setPreferredSize(new Dimension(50,250));

          //add adjustment listener in scrollbar

          s.addAdjustmentListener(this);

          // set size and visibility of frame

          setSize(400,400);

          setVisible(true);

        }

        //called whenever the scollbar value changed by using the following method

        public void adjustmentValueChanged(AdjustmentEvent e)

        {

            //update the title

            setTitle("Currentvalue"+e.getValue());

        }    

    //main method

    public static void main(String[] args) { 

    new adjustmentlistener(); 

}  

}

 output:-
Before scrolling check the title.
Fig: Before scrolling the title is Scrollbar_Adjustment.

After scrolling check the title.
                                  Fig: After scrolling the title is the Currentvalue 3(3 is the value of e).






for any query feel free to ask through comment.




Comments

Post a Comment