Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Lesson 9.5 Scrollbar

The ScrollBar Class

So far in this lesson, you've learned about composite objects--objects that are constructed much like you'd use the parts in your venerable Gilbert's Erector Set to build a tank or robot.

For your homework assignment, you're going to create a composite object--The Spinner class--using nothing but a Label and a Scrollbar. You're already well acquainted with the Label class, but the Scrollbar class is new to you. This is your introduction.

Creating ScrollBars

The Scrollbar class is a little more complex than the Label class [what class isn't] or the Button class. It requires a little more work to set it up and a little more work to interpret the information that it provides.

The Scrollbar class has a simple default constructor that is occassionally useful. You use it like this:

Scrollbar sb = 
    new Scrollbar();

The Scrollbar it creates is a vertical scrollbar that has a default value of 1, a minimum value of 0, and a maximum value of 100.

The applet Scrollbar1.java, which you can see running here, simply dumps the Scrollbar object to the System.out object whenever the Scrollbar is used. If you open up your Java Console after clicking on one of the Scrollbar buttons, you'll see something like this:

The output of the Scrollbar1 applet shown in Netscape's Java Console

Back to Top

Scrollbar Attributes

A Scrollbar is normally used to represent a single integer value inside a bounded range of values. This is often used to simulate an analog-type control by making the bounded range very large.

The Scrollbar has five different attributes that you'll commonly deal with. There are others, but you'll almost always work with these five:

  • Orientation: Can be Scrollbar.VERTICAL or Scrollbar.HORIZONTAL
  • value: The "current setting" of the Scrollbar. This will be an integer falling between the Scrollbar's minimum and maximum values.
  • minimum: The lowest value the Scrollbar can be set to.
  • maximum: The highest value the Scrollbar can be set to.
  • visible: The size of the Scrollbar "thumb".

The Working Constructor

The Scrollbar's working constructor allows you to set all five of these values like this:

Scrollbar slider = new Scrollbar(
    Scrollbar.HORIZONTAL,
    125,    // value
    25,     // visible
    0,      // minimum
    255);   // maximum

All of these should be pretty self explanatory except for the visible property, which determines how much of the Scrollbar "thumb" is visible. The visible property interacts with the maximum property to determine how large the Scrollbar value can be.

Here is an applet, Scrollbar2.java, that creates three horizontal Scrollbars using the working constructor. The range for each Scrollbar is identical [0-255], but the visible property is set to 5, 25, and 125 respectively. A Label is used to display the value of each Scrollbar as it is moved.

As you can see with a few moments ofexperimentation, the effective maximum value of a Scrollbar is it's maximum value minus the size of its thumb or visible property.

Back to Top

ScrollBar Events

Handling Scrollbar events can become complex because there are so many ways the user can manipulate them. 

The user can drag the thumb, click on the arrows at either end of the Scrollbar, or click on the region between the end-arrows and the thumb. The user can also manipulate the Scrollbar with they keyboard [on most platforms] using the arrow keys and the Page-up/Page-down keys.

Luckily, you don't have to deal with all of that complexity if you don't want to. All you really need to know is how to subscribe to Scrollbar events, and then, once an event occurs, how to retrieve the value from the Scrollbar

What about Java 1.0?
In Java 1.0, you have to override the handleEvent() method instead of subscribing to events. Your text has a description of how to do that. If we have time, I'll cover it in Lesson 13, "Advanced Inheritance." For right now, however, I'll assume you're using Java 1.1.

To subscribe to an event, you simply add implements AdjustmentListener to your class header, just as you did with ActionListener and ActionEvents

If your applet has some Buttons as well as Scrollbars, you can listen for both kinds of events by separating their names with a comma like this:

public MyClass extends Applet
          implements ActionListener,
                     AdjustmentListener
{
  // Attributes & Methods here
}

Just like ActionListener, AdjustmentListener requires you to write a single method. Here is its signature:

public void 
adjustmentValueChanged(AdjustmentEvent)
{
  // Code for AdjustmentEvent here
}

To actually subscribe to a particular Scrollbar's AdjustmentEvent, you must send the Scrollbar an addAdjustmentListener() message. If your applet implements AdjustmentListener, simply send this.

Here's a skeleton you can follow to get you started. The steps necessary for event-handling are highlighted.

Listening for Scrollbar Events
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SB extends Applet
      implements AdjustmentListener
{
  public void init()
  {
    Scrollbar sb = new Scrollbar();
    sb.addAdjustmentListener(this);
  }

  public void adjustmentValueChanged(
              AdjustmentEvent)
  {
    // Respond to sb events here
  }
}

Something to Talk About

Do you want to try a Scrollbar exercise? Here's one:
  • Download Scrollbar1.java
  • Compile it and run it in appletviewer. 
  • Using the JavaDocs, look up the other constructor offered by the Scrollbar class [the constructor is not discussed here]. 
  • Change Scrollbar1 to use the different constructor.

Please continue to the next section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation