You are here: Home / Topics / Program to use Scrollbar AWT control in Java

Program to use Scrollbar AWT control in Java

Filed under: Java on 2023-10-25 06:43:08

//  Program to use Scrollbar AWT control.

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

/*
<applet code="ScrollbarDemo" width="250" height="150">
</applet>
*/

public class ScrollbarDemo extends Applet implements AdjustmentListener, MouseMotionListener 
{
String msg = "";
Scrollbar vertSB, horzSB;

public void init() 
{
 setLayout(null);
 int width = Integer.parseInt(getParameter("width"));
 int height = Integer.parseInt(getParameter("height"));
 
 vertSB = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, height);
 horzSB = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, width);
 
 vertSB.setBounds(25, 15, 25, 125);
 horzSB.setBounds(60, 15, 125, 25);
 
 add(vertSB);
 add(horzSB);
 
 vertSB.addAdjustmentListener(this);
 horzSB.addAdjustmentListener(this);
 addMouseMotionListener(this);
}

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

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

public void mouseMoved(MouseEvent me) 
{
}

public void paint(Graphics g) 
{
 msg = "Vertical: " + vertSB.getValue();
 msg += ", Horizontal: " + horzSB.getValue();

 g.drawString(msg, 60, 150);
 g.drawString("*", horzSB.getValue(), vertSB.getValue());
}
}

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.