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 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:
You will notice that the command to set up the scroll bar consists of new Scrollbar() where the brackets contain 5 numbers:
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).
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;
}
}
Here is the result:
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.
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 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!
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.