Scrollbar control in java

Scrollbar

  • Scrollbar class is useful to create scrolls.
  • Scrollbars are used to select continuous values between a specified minimum and maximum.
  • It can be arranged vertically or scrollbar position for the selected element.
  • Constructor
    • Scrollbar()
    • Scrollbar(int orientation)
    • Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
  • Methods
    • int getValue()
    • void setValue(int position)
    • int getMaximum()
    • int getMinimum()
    • int getOrientation()
Example
import java.awt.*;
import java.awt.event.*;
class test extends Frame implements AdjustmentListener
{
String msg=” “;
Scrollbar sb;
test()
{
setLayout(null);
sb = new Scrollbar(Scrollbar.VERTICAL,0,30,0,400);
sb.setBounds(250, 50, 30, 200);
add(sb);
sb.addAdjustmentListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void adjustmentvalueChanged(AdjustmentEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(“Scrollbar position: “,20,150);
msg += sb.getValue();
g.drawString(msg, 20, 180);
msg=” “;
}
public static void main(String args[])
{
test t = new test();
t.setTitle(“My Scrollbar”);
t.setSize(400, 400);
t.setVisible(true);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *