Tuesday, 19 February 2019

scrollbar in java AWT - Online Help

Scrollbar in java AWT:

The scrollbar in java awt, lets the user graphically select a value by sliding knob. Scrollbar control represents a scroll bar component in order to enable user to select from range of values.




The constructor in java awt scrollbar:

Scrollbar class has three constructors. They are:

 ScrollBar()
It is used to create a scroll bar with a range of 0 to 100

ScrollBar(int orient)
It is used to create a scrollbar with a range of 0 to 100 and an initial value of 0 and a specified orientation. The orientation of scrollbar is as follows:

ScrollBar.HORIZONTAL- Orient the scrollbar horizontally.

ScrollBar.VERTICAL-  Orient the scrollbar vertically.

ScrollBar(int orient, int value, int width, int min, int max):
It is used to create a scrollbar with an initial value, width of the slider and minimum and maximum range of scrollbar.


Methods available in java AWT Scrollbar:

The most commonly used scrollbar() methods are as follows:

Void addAdjustmentListener(AdjustmentListener ae)
 This method is used to configure an event handler for the scroll bar.

Int getValue()
 This method is used to return the value of the scroll bar setting.

setMaximum(int max)
This method is used to set the maximum value of the scroll bar.

setMinimum(int min)
 This method is used to set the minimum value of the scroll bar.

setBackground(Color bgcolor)
 This method is used to set the background color of the scroll bar.

setValue(int val)
This method is used to set a value to the scroll bar.

Sample Program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet=”scroll” width=500 height=500>
</applet> */
public class scroll extends Applet implements AdjustmentListener
{
TextField t1;
Scrollbar s1,s2;
public void init()
{
s1=new Scrollbar(Scrollbar.HORIZONTAL,1,20,1,200);
add(s1);
s1.addAdjustmentListner(this);
s2=new Scrollbar(Scrollbar.VERTICAL,1,20,1,200);
add(s2)
s2.addAdjustmentListener(this);
t1=new TextField(20);
add(t1);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(e.getAdjustable()==s1 || e.getAdjustable()==s2)
{
t1.setText(“Horizontal”+s1.getValue()+”vertical”+s2.getValue());
}
}
}

No comments:

Post a Comment