A MIDlet that creates a simple program that has a numeric, modifiable gauge and opens a blank screen.
Create the SimpleTest.java
class.
Import the required classes.
import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
Set SimpleTest
to
extend MIDlet
and to implement CommandListener
. CommandListener
is
used by applications which need to receive high-level events from the implementation.
An application provides an implementation of a CommandListener
(typically
by using a nested class or an inner class) and then provides the instance
to the addCommand
method on a Displayable
in
order to receive high-level events on that screen. For more information, see CommandListener
in
the LCDUI API specification.
public class SimpleTest extends MIDlet implements CommandListener {
Define the commands, interfaces, and classes used by this MIDlet.
A Form
class
is a form of the Screen
class, containing a mixture of
items: images, read-only text fields, editable text fields, editable date
fields, gauges, choice groups, and custom items. In general, any subclass
of the Item
class may be contained within a form
.
The implementation handles layout, traversal, and scrolling. For more information, see Form
, Screen
,
and Item
in the LCDUI API specification.
private HelloCanvas myCanvas; private Form myForm;
The Gauge
class implements a graphical display,
such as a bar graph like in this example, of an integer value. It contains
a current value that lies between a zero and the maximum value, inclusive.
The MIDlet can control the current value and maximum value. The range of values
specified by the MIDlet may be larger than the number of distinct visual states
possible on the device, which means more than one value may have the same
visual representation. A TextField
class is an editable
text component that may be placed into a Form
. It can
be given a piece of text that is used as the initial value. For more information, see Gauge
and TextField
in the LCDUI API specification.
private Gauge myGauge; private TextField textField;
The Command
class is a construct that encapsulates
the semantic information of an action. The behavior that the command activates
is not encapsulated in this object. This means that the command contains only
information about “command”, not the actual action that happens when the command
is activated. The action is defined in a CommandListener
associated
with the Displayable
abstract class. Command
objects
are presented in the user interface and the way they are presented may depend
on the semantic information contained within the command. For more information, see Command
, CommandListener
, and Displayable
in
the LCDUI API specification.
Create Command
objects
that allows you to manipulate the MIDlet's functions.
private Command backCommand = new Command("Back", Command.BACK, 1); private Command messageCommand = new Command("Message", Command.SCREEN,1); private Command displayCommand = new Command("Display Message", Command.SCREEN,1); private Command exitCommand = new Command("Exit", Command.EXIT, 1); private Command showCommand = new Command("Show Levels", Command.SCREEN, 1);
Create a new SimpleTest
object. Create a new HelloCanvas
object.
Add the backCommand
and messageCommand
to
your new myCanvas
with the addCommand
method. For more information,
see addCommand
in the LCDUI API specification.
public SimpleTest(){ myCanvas = new HelloCanvas(); myCanvas.addCommand(backCommand); myCanvas.addCommand(messageCommand);
Next, create a new Form
object and initialize it
with the text "Gauge level". Also create a new Gauge
object
with the values shown below in the code segment. Use the append
method
to add your new Gauge
object and the TextField
object textField
created
previously in this step to your new Form
object. For
more information, see append
in
the LCDUI API specification.
myForm = new Form("Gauge level"); myGauge = new Gauge("Value", true, 120, 10); textField = new TextField("Enter number", "", 3, TextField.NUMERIC); myForm.append(myGauge); myForm.append(textField);
Use the addCommand
method to add the showCommand
, displayCommand
and
the exitCommand
to your myForm
.
myForm.addCommand(showCommand); myForm.addCommand(displayCommand); myForm.addCommand(exitCommand);
Set a CommandListener
for both the myCanvas
and
the myForm
objects.
myCanvas.setCommandListener(this); myForm.setCommandListener(this); }
Add the following three required MIDlet methods.
public void startApp(){ Display.getDisplay(this).setCurrent(myForm); } public void pauseApp(){} public void destroyApp(boolean a) throws MIDletStateChangeException{}
Create a commandAction
object
to give functionality to the Commands
created in step
4. Call the method notifyDestroyed
in case the exitCommand
command
is called by selecting the Exit function from the MIDlets GUI. If the messageCommand
is
called, use the newMessage
method from the myCanvas
object
(created in the next section) to return to open the new screen. For
more information, see notifyDestroyed
in
the LCDUI API specification.
public void commandAction(Command c, Displayable d){ if (c == exitCommand){ notifyDestroyed(); } if (c == messageCommand){ myCanvas.newMessage(); }
If the backCMD
is called, use the setCurrent
method
to return to the form (myForm
) from the menu, using the
getDisplay method. For
more information, see setCurrent
and getDisplay
in the LCDUI API specification.
The displayCommand
calls the start
method
from the myCanvas
object. This repaints the screen.
showCommand
uses the getString
method
to place the content of the textField
to the valueString
.
Use the value to manipulate myGauge
with the setValue
method.
If the value is less than zero, zero is used. If the current value is greater
than the maximum value, the current value is set to be equal to the maximum
value. For
more information, see getString
, setValue
, and setCurrent
in the LCDUI API specification.
if (c == backCommand){ Display.getDisplay(this).setCurrent(myForm); } if (c== displayCommand){ Display.getDisplay(this).setCurrent(myCanvas); myCanvas.start(); } if (c==showCommand){ String valueString = textField.getString(); int value = 0; if (!valueString.equals("")) { value = Integer.parseInt(valueString); } myGauge.setValue(value); } } }