Scrollbars
The Scrollbar class provides a user interface to scroll through a range of integer values. These values can then be associated with a range of colors, the months of a year or a variety of other uses. The Scrollbar's maximum and minimum values can be initialized as well as its minor changes (line increments) and major changes (page increments).
The following line will create a horizontal scrollbar.
Scrollbar colorscroll = new Scrollbar(Scrollbar.HORIZONTAL);
The following line will create a vertical scrollbar with a starting position of 0, a page size of 5, a minimum value of 0, and a maximum value of 255.
Scrollbar colorscroll = new Scrollbar(Scrollbar.VERTICAL, 0, 5, 0, 255);
There are three different parts of a scrollbar that allow you to select a value between the maximum and minimum in different ways. The arrows increment or decrement with the line update which can be set to a small unit. Its value is 1 by default. Clicking between the arrow and scroll box increments or decrements by a page value which is 10 by default. The box in the middle allows you to click and drag to traverse the scroll bar quickly from end to end.
The following methods may be used with scrollbars.
colorscroll.setLineIncrement(3); . int lineIncrement = colorscroll.getLineIncrement(); colorscroll.setPageIncrement(10); . int pageIncrement = colorscroll.getPageIncrement(); int minimum = colorscroll.getMinimum(); int maximum = colorscroll.getMaximum(); colorscroll.setValue(15); int currentPosition = colorscroll.getValue(); |
Set
the line increment. To determine the value of the line increment Set the page increment To determine the value of the page increment. To determine the minimum value To determine the maximum value To set the current position. To determine the current position |
Like the List class, the Scrollbar uses the handle event method and not the action method. A list of the possible values of evt.id for Scrollbar events follows:
Event.SCROLL_ABSOLUTE: The slider buttton is dragged.
Event.SCROLL_LINE_DOWN: The top or left arrow button is pressed.
Event.SCROLL_LINE_UP: The bottom or right arrow button is pressed.
Event.SCROLL_PAGE_DOWN: The area between the slider and the bottom or left arrow is clicked.
Event.SCROLL_PAGE_UP: The area between the slider and the top or right arrow is clicked.
The following example illustrates the use of the scrollbar to change the color of the square.
| Notice that ... When you click on the up or down
arrow If you drag the center (scroll) button you will change When you click on the region between Using the appropriate event, the user can quickly |
The code for the Applet is listed below
import java.applet.*;
import java.awt.*;
public class scroller extends Applet
{
Scrollbar colorscroll = new
Scrollbar(Scrollbar.VERTICAL,0,0,0,255);
int redvalue = 175;
Label scrollvalue = new Label
(String.valueOf(redvalue));
public void init()
{
add(new Label("Scroll to change the
color of the box."));
add(colorscroll);
colorscroll.setPageIncrement(50);
colorscroll.setValue(175);
add(new Label("value of the scrollbar ="));
add(scrollvalue);
}
|
Import all the facilities of
the AWT and applet that Java has to offer. Create an applet called scroller. Colorscroll is an instance of the Scrollbar class. It is vertical, starting position is 0, page size is 0, minimum is 0 and maximum is 255. A label displays the value of the red color. The init method adds a label and the scrollbar to the applet. The PageIncrement is set and the current value of the scroll position. Another label displays the current scroll value. |
public void paint (Graphics g)
{
g.setColor(new Color(redvalue,0,0));
g.fillRect(90,90,100,100);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
redvalue = colorscroll.getValue();
scrollvalue.setText(String.valueOf
(redvalue));
repaint();
return true;
}
return false;
}
}
|
The paint method fills a square with the
shade corresponding to the redvalue. The handleEvent
method sets the redvalue to the current value of the
colorscroll. |
The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "scroller.java".
After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the labels and colored square. (width-301 and height-301).
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
Back to the Java AWT Components Page