October 15, 2001
Brought to you by:
Check It Out!
|
Handling penDown() Events
By extending com.sun.kjava.Spotlet, ContactBook is able to receive a variety of user interface events by overriding Spotlet event handlers. In this case, we are only interested in knowing when the user has clicked one of our buttons. We'll override Spotlet.penDown() to determine this.
public void penDown(int x, int y) {
//exit btn pressed?
if (exitBtn.pressed(x,y))
System.exit(1);
//add btn pressed?
else if (addBtn.pressed(x,y)) {
AddContact ac = new AddContact(this);
}
//beam btn pressed?
else if (beamBtn.pressed(x,y)) {
BeamContact ba = new BeamContact(this);
}
//receive btn pressed?
else if (receiveBtn.pressed(x,y)) {
ReceiveContact ra = new ReceiveContact(this);
}
//search btn pressed?
else if (searchBtn.pressed(x,y)) {
SearchContact sc = new SearchContact(this);
}
}
Spotlet.penDown() receives the x and y screen coordinates, in pixels, where the stylus was pressed. The rest of the code calls com.sun.kjava.Button.pressed(), which returns Boolean true if (x,y) are within the Button boundaries, to determine if a Button was pressed. If a Button was pressed, one of the other user interface classes is called. We will discuss each of these in detail. The only exception is the quit Button, which terminates the program.
Let's take a look at the class, AddContact, that gets instantiated if the top-most Button is pressed.
AddContact
|
public class AddContact
extends com.sun.kjava.Spotlet |
AddContact is the class that enables the user to add contact entries into the custom Palm database, JavaXMLProgRef. A custom Palm database, also called a user-defined database, is a database that is not one of the system-defined databases: the address, date book, memo pad, and to do list databases. We have chosen not to use the address database because it defines its own record format for contacts; we would not be able to natively store XML if we used it. Creating our own user-defined database allows us to store bytes in any format we choose, and we choose XML, of course!
AddContact displays a data entry form to the user for the following fields:
- First name
- Last name
- Address
- City
- State/province
- Zip/postal code
- Country
- Phone number
Three buttons are also displayed:
- quit return control to class ContactBook without saving the data entered (if any) in the form
-
save & quit creates a new record in the JavaXMLProgRef database using current form values, then returns control to ContactBook and its screen
-
save & continue creates a new record in the JavaXMLProgRef database using current form values, then erases all form field values so another contact can be entered.
Let's look at a screenshot:

We'll examine three parts of class AddContact:
- The constructor
- Saving the generated XML as a new record in the JavaXMLProgRef database
-
Generating XML from the form's eight com.sun.kjava.TextField objects
Constructor
The constructor allocates objects for the com.sun.kjava.Button and com.sun.kjava.TextField variables declared with private class scope. It also registers as a listener for user interface events via the com.sun.kjava.Spotlet event handlers.
public class AddContact extends Spotlet {
private Button quitBtn, saveQuitBtn, saveAddMoreBtn;
private ContactBookSpotlet _owner;
private TextField fname, lname, address, city, state, zip,
country, phone;
public AddContact(ContactBookSpotlet owner) {
//register for events
register(NO_EVENT_OPTIONS);
_owner = owner;
quitBtn = new Button("Quit", 1, 140);
saveQuitBtn = new Button("Save&Quit", 33, 140);
saveAddMoreBtn = new Button("Save&Continue", 92, 140);
fname = new TextField("First Name", 5, 10, 150, 10);
lname = new TextField("Last Name", 5, 25, 150, 10);
address = new TextField("Address", 5, 40, 150, 10);
city = new TextField("City", 5, 55, 150, 10);
state = new TextField("State", 5, 70, 150, 10);
zip = new TextField("Zip", 5, 85, 150, 10);
country = new TextField("Country", 5, 100, 150, 10);
phone = new TextField("Phone", 5, 115, 150, 10);
paint(); //paint the screen
}
The TextField constructor's five arguments are similar. The first is the text or title that gets put in front of the text field (a colon is automatically appended to the supplied java.lang.String by the TextField class). The next four elements are measured in screen pixels and are, respectively, the upper (x,y) coordinates of the TextField, and the width (length) and height of the TextField. It's interesting to note that this simple UI element does not scroll if characters are entered after the full width of the element is occupied. Therefore, the maximum number of characters held by a TextField is determined by its width. Another oddity is that the Palm OS's use of a non-fixed-width font makes this maximum character length vary based upon the width of the characters entered (for example, less m's can be entered than l's)!
The last instruction in the constructor calls the paint() method, which we will skip since it is very similar to ContactBook.paint() (see Painting the Screen).