AdjustmentEvent


Prev Tutorial Next Tutorial

Event handling for Scrollbar

AdjustmentEvent class and AdjustmentListener interface are used for handling event for scrollbar.

This event class and Listener interface are associalted with Scrollbar component adjustmentValueChanged() contains the logic to perform any operation if scrollbar position is changed.

GUI ComponentEvent classListener InterfaceMethod (abstract method)
ScrollbarAdjustmentEventAdjustmentListenerpublic void adjustmentValueChanged(ItemEvent e)

Example of AdjustmentEvent

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

class AdjustmentEventEx extends Frame implements AdjustmentListener
{
int rval=0,gval=0,bval=0;
Scrollbar sr,sb,sg;
Panel ps;
AdjustmentEventEx()
{
	ps=new Panel();
	Label l1=new Label("Red");
	Label l2=new Label("Green");
	Label l3=new Label("Blue");
	sr=new Scrollbar(Scrollbar.HORIZONTAL,0,5,0,255);
	sg=new Scrollbar(Scrollbar.HORIZONTAL,0,5,0,255);
	sb=new Scrollbar(Scrollbar.HORIZONTAL,0,5,0,255);
	ps.add(l1);
	ps.add(sr);
	ps.add(l2);
	ps.add(sg);
	ps.add(l3);
	ps.add(sb);
	add("South",ps);
	sr.addAdjustmentListener(this);
	sg.addAdjustmentListener(this);
        sb.addAdjustmentListener(this);
	setSize(400,400);
	setVisible(true);
	}
	public void adjustmentValueChanged( AdjustmentEvent e)
	{
		if(e.getSource().equals(sr))
		{
			rval=sr.getValue();
			setBackground(new Color(rval,bval,gval));
		}
		if(e.getSource().equals(sg))
		{
			gval=sg.getValue();
			setBackground(new Color(rval,gval,bval));
		}
		if(e.getSource().equals(sb))
		{
			bval=sb.getValue();
			setBackground(new Color(rval,gval,bval));
		}
	}

	public static void main(String[] args) 
	{
		AdjustmentEventEx ae=new AdjustmentEventEx();
	}
}

Download code Click

adjustmentevent example
Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements