A scrollbar (Also known as a handle in the very first GUIs) is a graphical object in a GUI with which continuous text, pictures or anything else can be scrolled including time in video applications, i.e., viewed even if it does not fit into the space in a computer display, window, or viewport.
Usually designed as a long rectangular area on one or two sides of the viewing area, containing a bar (or thumb) that can be dragged along a trough (or track) to move the body of the document as well as two arrows on either end for precise adjustments. The "thumb" has different names in different environments: on the Macintosh it is called a "scroller"; on the Java platform it is called "thumb" or "knob"; Microsoft's .NET documentation refers to it as "scroll box" or "scroll thumb"; in other environments it is called "elevator", "quint", "puck", "wiper" or "grip". Additional functions may be found, such as zooming in/out or various application-specific tools. The thumb can also sometimes be adjusted by dragging its ends. In this case it would adjust both the position and the zooming of the document, where the size of the thumb represents the degree of zooming applied. A thumb that completely fills the trough indicates that the entire document is being viewed, at which point the scrollbar may temporarily become hidden.
Scrollbars can be seen as a computer representation of a thumb, with which you thumb through pages of documents.
A scrollbar should be distinguished from a slider which is another object that works in a similar fashion, the difference being that the slider is used to change values, and does not change the display or move the area that is shown.
Well, it's my own term - not one that you will see in any Java textbook. I use it to refer to a variety of input items in a Java applet, such as buttons (which you have already met) scroll bars, text fields and text areas. The following Java applet shows a few of these:
We have already met buttons, so I shan't be talking much about them here. Like buttons, scroll bars, text areas etc. are automatically placed as close to the top of the screen as possible, and in the centre. As you add more, they gradually fill up the screen downwards. Here is the code for the applet above:
import java.applet.*;
import java.awt.*;
public class furn1 extends Applet
{ public Scrollbar my_bar;
public TextField my_textline;
public TextArea my_blurb;
public void init ()
{ my_bar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100);
add(my_bar);
my_textline = new TextField(12);
add(my_textline);
my_blurb = new TextArea(7, 5);
add(my_blurb);
}
}
You can see that there is no paint() method. The fact that the screen furniture is declared, set up and added to the screen in method init() is enough to make them appear. Of course, the items added don't actually do anything - try dragging the scroll bar or typing text into the two text slots.
One thing that all the items have in common with buttons is the way in which they are declared. Each item has to be declared using the word public or private (I have chosen public here) and the type of the item (Scrollbar, TextField, TextArea) and the name.
Then the item has to be declared in the init() method, using the word new followed by the item type. The parameters passed to the type vary slightly from one item to the next. In the case of buttons, you will remember that the caption of the button was passed:
click_on_me = new Button("This button starts the program");
Here, different things are passed in the case of scroll bars, text areas etc. These are dealt with in their own sections below. Finally, each item has to be put onto the screen, using the add() instruction. If you don't, the item simply won't appear, even though it has been declared.
Scroll Bars
Scroll bars are declared using the Scrollbar type (Note that there is no space in the middle of that, and that the 'b' is lower case). A scroll bar consists of a small bar that can be dragged from one end of its "slot" to the other, and whenever the user does so, two things happen:
* Changing the scroll bar causes an event to happen (i.e. the program is alerted of the fact).
* The scroll bar returns a number to the program.
Setting up the scroll bar
You will notice that the command to set up the scroll bar consists of new Scrollbar() where the brackets contain 5 numbers:
* The first number is usually given in the form of a property, either Scrollbar.VERTICAL or Scrollbar.HORIZONTAL. These are in fact both numbers, although they are stored as values inside the Scrollbar class. Although you could try numbers here (I think it's 1 for vertical and 0 for horizontal, but I'm not sure) - it's probably easier to use the constants that they provide. If you are going to use the constant values, you must put them in upper case letters. Although Scrollbar.HORIZONTAL is correct, Scrollbar.Horizontal is not.
* The second number is the initial value that the scroll bar holds. Remember, the scroll bar represents a number that the user can alter, and that number has to start with a particular value. This starting value will also depend on what range of values are specified (the last two parameters, as explained below). There is no point in declaring that the scroll bar must return values in the range 0 to 100, and then start if off with a value of 200. I'll leave you to find out what happens if you try that.
* The third number is the Large Step Change. As well as dragging the scroll bar back and forth along its track, we can click on the arrows at the end. Every time that you do so, the value represented by the scroll bar will change by the amount specified in this parameter.
* The last two numbers indicate the minimum and maximum values (in that order) that the scroll bar can return. For instance, if you set these parameters to 100 and 200, then the scroll bar would only give numbers in that range: 100 when the slider was at one end of the track, 200 when it was at the other end, and some number in between on other occasions.
Here's an example of a scroll bar being set up:
Scrollbar indicate_strength = new Scrollbar(Scrollbar.HORIZONTAL, 25, 5, 0, 100);
This sets up a horizontal scroll bar which returns a range of numbers from 0 to 100. The scroll bar starts one quarter of the way along the track (i.e. at figure 25) and if we clicked on the arrows at the end of the track, it would return numbers increasing or decreasing by 5 (i.e. 30, 35, 40, 45 etc. or 20, 15, 10, 5, 0).
Reading values from the scroll bar
When the user drags the scroll bar to one side, or clicks on the arrows at the ends to change its values, the scroll bar should immediately hand a number back to the program and get the program to activate it. As you may have guessed, we need to write a special method, called handleEvent(), which we don't call specifically, but which Java calls automatically when the scroll bar is clicked on.
You may well be asking "Why doesn't Java use the action() method, which it uses to deal with buttons?" Well, that is a very good question, and I can't think of a sensible answer! It is a mystery to me also - I can't think of any reason why it should (although I dare say there is one).
The method handleEvent() is defined according to the following pattern. There is a single parameter, which I have chosen to call e() (but which you can call any name you like), which is used to distinguish between different events that may call handleEvent(). If you only have one possible event (e.g. there is only one scrollbar and nothing else that could call handleEvent()), then you don't have to refer to this parameter inside the body of the method (although it must still be included in the definition):
public boolean handleEvent (Event e)
{ // Statements in here to handle various events
return true;
}
Just like action(), handleEvent() is defined as returning a boolean value, so it should end in the statement return true. Inside the body of the method you include the statements to handle the event that has just occurred. In this case, we have a scrollbar called indicate_strength and we want to read its value. Scrollbar objects contain a method built into them called getValue() (note the capital V) which returns the value that the scrollbar is set to. Here is a sample program that reads the value of the scrollbar and displays it on the screen:
import java.awt.*;
import java.applet.*;
public class scrollDemo extends Applet
{ public int value = 0; // The value of the scrollbar
// will appear in this variable
public Scrollbar indicate_strength;
// Set up the scrollbar
public void init ()
{ indicate_strength = new Scrollbar(Scrollbar.HORIZONTAL, 25, 5, 0, 100);
add(indicate_strength);
}
public void paint (Graphics g)
{ g.drawString("Scrollbar value = " + value, 10, 40);
}
// The user has altered the scrollbar, so read its new value
public boolean handleEvent (Event e)
{ value = indicate_strength.getValue();
repaint();
return true;
}
}
The method handleEvent() contains a call to repaint() which updates the screen with the new value of the scrollbar. You would use the parameter e to distinguish between, for example, two scrollbars. The following example program has two scrollbars, termed leftbar and rightbar and displays their combined values:
import java.awt.*;
import java.applet.*;
public class scroll2 extends Applet
{ public String message = "";
public Scrollbar leftbar, rightbar;
public int leftvalue = 0, rightvalue = 0;
public void init ()
{ leftbar = new Scrollbar(Scrollbar.VERTICAL, 3, 1, 0, 10);
rightbar = new Scrollbar(Scrollbar.VERTICAL, 8, 1, 0, 10);
add(leftbar);
add(rightbar);
}
public void paint (Graphics g)
{ g.drawString("Total value = " + (leftvalue + rightvalue), 10, 70);
g.drawString(message, 10, 85);
}
// The user has altered the scrollbar, so read its new value
public boolean handleEvent (Event e)
{ if (e.target == leftbar)
{ leftvalue = leftbar.getValue();
message = "You altered the left bar.";
}
else
{ rightvalue = rightbar.getValue();
message = "You altered the right bar.";
}
repaint();
return true;
}
}
Inside the handleEvent() method, we use an if statement (which we will meet in a later section) to decide whether the left bar has been clicked or the right one. The appropriate variable is set, and a string variable is also set to indicate which bar was clicked.
Text Fields
A text field is a slot which is one line of text high. It is used for entering (generally) small quantities of text into programs. You can type as much text as you like into the text field - it scrolls as you type beyond the end of the slot - although if you want to enter large quantities of text, it is better to use a text area.
Text fields are handled in a similar way to scroll bars. They are set up at the start of the program and then values can be read from them at any point. Text fields, like other screen furniture, appear as close to the top central point of the applet as possible, with the applet "filling up" as more items are added.
Setting up a text field is simpler than setting up a scroll bar, as you only have to specify the number of columns in the field (i.e. its width). For example, the following command sets up a text field with 12 columns:
TextField type_age;
type_age = new TextField(12);
add(type_age);
Note that we declare the text field as being of type TextField (yes, that has a capital T and F), after which it can be set up and added to the screen. The declaration of the text field should be done in the earliest parts of the program, where the variables are declared. The setting up and adding of the text field should be done in the action() method. This is exactly the same procedure as for any other screen furniture, such as scroll bars.
Reading the contents of text fields is done in a similar way to scroll bars also, except that the method built into the TextField class is called getText() rather than getValue. Please, note, however, that this method returns a string rather than a number (more on this in a moment). Here is a simple example of a text field - simply enter any text into the text field and click on the button. The computer will then mimic what you typed below.
import java.applet.*;
import java.awt.*;
public class tFieldDemo extends Applet
{ public TextField type_something;
public Button do_it;
String repeat_mindlessly = "";
public void init ()
{ type_something = new TextField(20);
add(type_something);
do_it = new Button("Repeat this");
add(do_it);
}
public void paint (Graphics g)
{ g.drawString("You typed : " + repeat_mindlessly, 20, 50);
}
public boolean action (Event e, Object args)
{ repeat_mindlessly = type_something.getText();
repaint();
return true;
}
}
The parts of the program concerning the text field are shown in red. You will find that each of the lines in red corresponds to one of the things that you red about above.
Text Areas
Text areas are similar to text fields except they cover a certain number of columns and rows. They are set up as follows:
TextArea t;
t = new TextArea(15, 50);
The first number represents the number of rows (lines of text) and the second number the number of columns. Simply type the text in the box as you would into a normal word processor. As with text fields, there is a getText() method built into text area. Here is a duplicate of the applet that you saw above with a few changes made:
import java.applet.*;
import java.awt.*;
public class tAreaDemo extends Applet
{ public TextArea type_something;
public Button do_it;
String repeat_mindlessly = "";
public void init ()
{ type_something = new TextArea(10,40);
add(type_something);
do_it = new Button("Repeat this");
add(do_it);
}
public void paint (Graphics g)
{ g.drawString("You typed : " + repeat_mindlessly,20,270);
}
public boolean action (Event e, Object args)
{ repeat_mindlessly = type_something.getText();
repaint();
return true;
}
}
The text doesn't work wrap in the text area: You have to press Enter at the end of each line to move to the next one. There are several methods associated with text areas (and scroll bars, buttons and text fields, for that matter) that I haven't mentioned, but I haven't yet come across one that wraps text in a text area. Sorry!
Removing Screen Furniture
There may well be circumstances when you want to make various items of screen furniture disappear from the screen. For instance, a button may only have a valid function at some points in a program and be meaningless at other points, in which case it would be useful to make it disappear.
There is an instruction similar to add() which makes an item of screen furniture disappear from the screen. It doesn't remove all reference to the object, but simply makes it invisible! In the following applet, I set up two buttons. The first button makes the second one disappear and appear. The second button does nothing (it is simply there as an item of screen furniture). The boolean variable button_present is used only to keep track of whether the button is visible on the screen.
The source code for the applet should make its working clear:
import java.awt.*;
import java.applet.*;
public class testRemove extends Applet
{ Button a, b;
boolean button_present = true;
public void init()
{ a = new Button("Click here!");
add(a);
b = new Button("Hello");
add(b);
}
public boolean action (Event e, Object args)
{ if (e.target == a)
{ if (button_present == true)
{ remove(b);
button_present = false;
}
else
{ add(b);
button_present = true;
}
}
return true;
}
}
You will notice that the properties that the button possesses are not cancelled by the remove() instruction (or the add() instruction if the button is then redisplayed). The button still has the same caption (Hello) however many times you remove it and then add it again.