|
Wireless Java. Developing with J2ME Authors: Knudsen J. Published year: 2000 Pages: 39-40/129 |
The remainder of this chapter and all of Chapter 6 are devoted to Screen and its subclasses, which is the left branch of the hierarchy shown in Figure 5-1. Screen is the base class for all classes that represent generalized user interfaces.
Canvas , by contrast, is a base class for specialized interfaces, such as those for games . Canvas will be fully covered later, in Chapter 9.
In the coming sections, we'll explore each of Screen 's child classes. Here, I'll briefly describe what all Screen s have in common: a title and a ticker. The title is just what you expect: a string that appears at the top of the screen. A ticker is simply a bit of text that scrolls across the top of a Screen , named after old-fashioned stock tickers.
The title is a text string displayed at the top of the screen. As you saw in Figure 5-3, the title of the screen is "TextBox." Subclasses of Screen have constructors that set the title, but the title may also be accessed using the following methods :
public void setTitle(String newTitle) public String getTitle()
The ticker is just as easy to access:
public void setTicker(Ticker newTicker) public Ticker getTicker()
The Ticker class is a simple wrapper for a string. To add a ticker to a screen, then, you would do something like this:
// Screen s = ...
Ticker ticker = new Ticker("This is the ticker message!");
s.setTicker(ticker);
Figure 5-5 shows a ticker in action. The full text of the ticker is "A ticker scrolls slowly."
Figure
5-5:
A ticker scrolls across the top of a
screen.
|
Team-Fly
|
The simplest type of screen is the TextBox , which you've already seen in action. TextBox allows the user to enter a string. Keep in mind that on a garden-variety MID, text input is a tedious process. Many MIDs only have a numeric keypad, so entering a single character is a matter of one, two, or three button presses. A good MIDlet requires minimal user input.
That said, your MIDlet may need some kind of input-perhaps a Zip code, or a short name , or some kind of password. In these cases, you'll probably want to use a TextBox .
A TextBox is created by specifying four parameters:
public TextBox(String title, String text, int maxSize, int constraints)
The title is used as the screen title, while text and maxSize determine the initial text and maximum size of the text box. Finally, constraints can be to restrict the user's input. Constants from the TextField class are used to specify the type of input required:
ANY allows any type of input.
NUMERIC restricts the input to numbers .
PHONENUMBER requires a telephone number.
EMAILADDR input must be an e-mail address.
URL input must be a Web address.
PASSWORD characters are not shown when entered; generally , they are represented by asterisks .
If you don't want the TextBox to perform any validation, use ANY or its numerical equivalent, 0, for the constraints parameter in the constructor.
The PASSWORD constraint may be combined with any of the other constraints using the OR operator. For example, to create a TextBox that constrained input to an e-mail address but wanted to keep the entered data hidden, you would do something like this:
Displayable d = new TextBox("Email", "", 64,
TextField.EMAILADDR TextField.PASSWORD);
If you think about it, though, PASSWORD is probably more trouble than it's worth. The point of PASSWORD fields, at least on desktop machines, is to keep someone walking past your computer screen from seeing your secret password. For every character you enter, the password field shows an asterisk or some other symbol. As you type your secret password, all that shows up on the screen is a line of asterisks (so casual observers cannot see your password). On mobile phones and other small devices, this is less of a concern, as the screens are smaller and much more difficult to read than a typical desktop monitor.
Furthermore, the difficulty of entering data on a small device means that it will be hard to correctly enter passwords typing blind. Mobile phones, for example, typically require pressing keys several times to enter a single letter. On Sun's J2MEWTK emulator, pressing the '7' key twice enters the letter 'Q.' On a real device, you would have to enter a password "gandalf" with the following sequence of key presses: 4, 2, 6, 6, 3, 2, 5, 5, 5, 3, 3, 3. Without visual feedback, it would be extremely easy to make a mistake when entering a password. ("Did I press the 5 key two times or three times?") The J2MEWTK emulator shows the current character but previously typed characters are shown as asterisks. Good passwords typically have mixed case, numbers, and possibly punctuation; these would be hard to get right.
|
Team-Fly
|
|
Wireless Java. Developing with J2ME Authors: Knudsen J. Published year: 2000 Pages: 39-40/129 |
![]() Macromedia ColdFusion MX 7 Web Application Construction Kit | ![]() Advanced Macromedia ColdFusion MX 7 Application Development |