Thursday 30 August 2012

Handling scroll bar



import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
/*<applet code=scroller.class width=350 height=400></applet>*/
public class scroller extends Applet implements AdjustmentListener                                                   
                                                                        
{
  TextField text1,text2;
  Label lab1,lab2;
  Scrollbar scroll1,scroll2;
  Button reset;
  public void init()
   {
     lab1=new Label("Horizontal scrollbar :",Label.CENTER);
     add(lab1);
     text1=new TextField(25);
     add(text1);
     lab2=new Label("   Vertical scrollbar :",Label.CENTER);
     add(lab2);
     text2=new TextField(25);
     add(text2);
     scroll1=new Scrollbar(Scrollbar.HORIZONTAL,1,10,1,100);
     add(scroll1);
     scroll1.addAdjustmentListener(this);
     scroll2=new Scrollbar(Scrollbar.VERTICAL,1,10,1,100);
     add(scroll2);
     scroll2.addAdjustmentListener(this);
    }

public void actionPerformed(ActionEvent e)
  {
             if(e.getSource()==reset)
                {
                scroll1.setValue(0);
                scroll2.setValue(0);
               } 
   }

 public void adjustmentValueChanged(AdjustmentEvent e)
  {
                if(e.getAdjustable()==scroll1)
                  {
                                scroll1.setValue(scroll1.getValue());
                                text1.setText("Horizontal position: "+scroll1.getValue());
                   }
                if(e.getAdjustable()==scroll2)
                  {
                                 scroll2.setValue(scroll2.getValue());
                                 text2.setText("Vertical position: "+scroll2.getValue());
                   }
   }

No comments:

Post a Comment