public class FetchPage extends MIDlet
implements CommandListener {
private Display display;
private Command cancelCommand;
private Command doneCommand;
private Command fetchCommand;
private TextField textField;
public FetchPage() throws Exception {
// Get Display object.
display = Display.getDisplay(this);
cancelCommand = new Command("CANCEL", Command.CANCEL, 1);
doneCommand = new Command("DONE", Command.CANCEL, 1);
fetchCommand = new Command("FETCH", Command.SCREEN, 1);
// ... ...
}
public void startApp() {
// Form with TextField to ask for URL.
Form form = new Form("Fetch Web Page");
textField = new TextField("URL:", "",
80, TextField.ANY);
form.append( textField );
// Add two buttons on the form.
form.addCommand(cancelCommand);
form.addCommand(fetchCommand);
// The CommandListener is this MIDlet itself.
form.setCommandListener( (CommandListener) this);
// Display the form on the LCD screen.
display.setCurrent(form);
}
// ... ...
public void commandAction(Command command,
Displayable screen) {
if (command == cancelCommand) {
// ... ...
} else if (command == doneCommand) {
// ... ...
} else if (command == fetchCommand) {
// Fetch the url and store the result in
// String pageContent. Refer to Listing 2 for details.
// TextBox is a Screen object.
TextBox textBox = new TextBox(url, null,
length, TextField.ANY);
// Display fetched content in textBox.
textBox.setString( pageContent );
// Add a button to the textBox screen.
textBox.addCommand(doneCommand);
// Event handle is this class itself.
textBox.setCommandListener( (CommandListener) this);
// Display the result screen.
display.setCurrent(textBox);
}
}
}
End of Listing