For information about the design and functionality of the MIDlet, see section Design.
The eSWT Hello World MIDlet consists of the HelloWorld
class, which defines the appearance of the UI, and an eSWT MIDlet starter, which creates the eSWT UI thread and also manages the MIDlet lifecycle.
The HelloWorld
class defines the
look and feel of the MIDlet. It simply lays out the text and adds
the exit command for exiting the MIDlet.
To implement the HelloWorld
class, add the following code:
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ercp.swt.mobile.Command; public class HelloWorld extends SWTMIDlet { public void runUI(Display display) { Shell shell = new Shell(display); Command exitCommand = new Command(shell, Command.EXIT, 0); exitCommand.setText("Exit"); exitCommand.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent arg0) { exit(); } public void widgetDefaultSelected(SelectionEvent arg0) { } }); shell.setLayout(new FillLayout()); Label label = new Label(shell, SWT.HORIZONTAL); label.setText("Hello, world!"); shell.open(); } }
To make developing eSWT MIDlets faster and
easier, the Java Developer's Library provides an eSWT MIDlet starter,
consisting of two classes, which you can use as a basis for implementing
MIDlets with eSWT UIs. The starter creates the eSWT UI thread and
also manages the MIDlet lifecycle for you. If supported by the device,
the starter uses the org.eclipse.ercp.swt.midp.UIThreadSupport
class to safely obtain a platform-optimized thread for the eSWT
UI. Otherwise, the starter creates a random thread for the UI.