For better usability, you can add a few additional features for text input and error handling. The file manager in this application uses a text input system during the rename operation.
The InputScreen
class is an additional class
for text input. It is launched from the FileSelector
class in situations where a new folder is created or a file
renamed.
The InputScreen
class contains a TextField
and standard OK / CANCEL Commands
. The entered text is returned to the FileSelector
as the new file or folder name.
To implement InputScreen
class:
Create the InputScreen.java
class file.
Import the required packages and classes.
import javax.microedition.lcdui.*;
Set InputScreen
to extend Form
and implement CommandListener
. Create the Commands
and
other variables.
class InputScreen extends Form implements CommandListener { private final ImageViewerMIDlet midlet; private final TextField inputField = new TextField("Input", "", 32, TextField.ANY); private final Command okCommand = new Command("OK", Command.OK, 1); private final Command cancelCommand = new Command("Cancel", Command.OK, 1);
Make the InputScreen
available in the MIDlet main view.
InputScreen(ImageViewerMIDlet midlet) { super("Input"); this.midlet = midlet; append(inputField); addCommand(okCommand); addCommand(cancelCommand); setCommandListener(this); }
Create methods
for setting up the TextField
and to read the text String
.
public void setQuestion(String question, String text) { inputField.setLabel(question); inputField.setString(text); } public String getInputText() { return inputField.getString(); }
Implement the commandAction
method.
public void commandAction(Command command, Displayable d) { if (command == okCommand) { midlet.input(inputField.getString()); } else if (command == cancelCommand) { midlet.cancelInput(); } } }
The ErrorScreen
class creates an Alert
of type ERROR
, which
is shown in situations where the MIDlet cannot, for example, access
a certain directory. The exact message String
is
entered in the showError
method.
To implement
the ErrorScreen
class:
Create the ErrorScreen.java
class file.
Import the required packages and classes.
import javax.microedition.lcdui.*;
Set ErrorScreen
to extend Alert
. Create the
required variables.
class ErrorScreen extends Alert { private static Image image; private static Display display; private static ErrorScreen instance = null;
Define the Alert
properties and create a method for displaying it.
private ErrorScreen() { super("Error"); setType(AlertType.ERROR); setTimeout(5000); setImage(image); } static void init(Image img, Display disp) { image = img; display = disp; } static void showError(String message, Displayable next) { if (instance == null) { instance = new ErrorScreen(); } instance.setTitle("Error"); instance.setString(message); display.setCurrent(instance, next); } }