Login    New User    Forgot Password    Help      

Scroll Bar

Java Tutorials

Example

// scroll bars
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="control7" width=250 height=300>

</applet>
*/
public class control7 extends Applet implements
AdjustmentListener,MouseMotionListener
{

String msg="";

Scrollbar vbar,hbar;

public void init()

{
int width=Integer.parseInt(getParameter("Width"));
int height= Integer.parseInt(getParameter("height"));

vbar=new Scrollbar(Scrollbar.VERTICAL,0,1,0,height);
hbar=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,width);

add(vbar);
add(hbar);

vbar.addAdjustmentListener(this);
hbar.addAdjustmentListener(this);

addMouseMotionListener(this);

}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
repaint();
}

public void mouseDragged(MouseEvent me)
{
int x=me.getX();
int y=me.getY();
vbar.setValue(y);
hbar.setValue(x);
repaint();
}

public void mouseMoved(MouseEvent me)
{

}

public void paint(Graphics g)
{
msg="Vertical "+ vbar.getValue();
msg+="Horizontal" + hbar.getValue();
g.drawString(msg,6,160);
g.drawString("*",hbar.getValue(),vbar.getValue());
}
}

Output

Output in Applet

Java Tutorials