Java Reference
In-Depth Information
return new Label(style, &Hello World");
}
return null;
}
We simply return a
Label
here, which could be done using a static
<label>
element in the XML file. However, by handing over the creation
of the UI to the script, we can understand how the framework functions
and also learn how to create a dynamic user interface. For example,
we could create a different message depending on whether it's morning,
afternoon or evening.
Let's now get back to our class variables,
CMD_BACK
and
BACK
. These
are used to create a
MenuItem
with the label Back, which is displayed on
the right softkey of a device, indicating that the user may return from the
widget to the dashboard. The actual work of placing the command on the
softkey is performed by the
getSoftKey()
method, part of a widget's
lifecycle; this is called by the framework so we can return whatever
commands we'd like to be placed on the softkeys:
MenuItem getSoftKey(Shell shell, Component focused, int key)
{
// let's change only the right softkey, whose id is SOFTKEY_BACK
if (key == SOFTKEY_BACK)
{
return BACK;
}
return null;
}
Last but not least, we implement the
actionPerformed()
method,
called by the framework when the softkeys are pressed:
void actionPerformed(Shell shell, Component source, int action)
{
if (action == CMD_BACK)
{
popShell(shell);
}
}
If the event passed to our implementation is
CMD_BACK
, that means
the right softkey has been pressed and the user wishes to exit the widget.
We then call the
popShell()
method, which sends the widget back to
the minimized view and frees resources.
A.3.4 Compiling and Running a Widget
After saving the file, let's open a command prompt and compile it:
devkit compile helloworld
\
helloworld.he