In our test MIDlet we are using BorderLayout for the form and laying out various components to different sides of this layout. The following code snippet of the demo MIDlet demonstrates the use of image and also of components like labels and buttons, and of the command being assigned to the mobile device's soft keys.
The Emulator executes the LWUIT Demo App, and by clicking the soft-key button under Exit, the app can be closed.
public class FirstMIDlet extends MIDlet implements ActionListener { private Form mForm; public void startApp() { if (mForm == null) { Display.init(this); Form f = new Form("LWUIT Demo App"); f.setLayout(new BorderLayout()); try{ Resources res = Resources.open("/myresourceFile.res"); Image i = res.getImage("duke"); Label bottomText = new Label(i); bottomText.setAlignment(Component.CENTER); bottomText.setText("Welcome to LWUIT!!"); bottomText.setTextPosition(Component.BOTTOM); Container buttonBar = new Container(new BoxLay out(BoxLayout.X_AXIS)); out(BoxLayout.X_AXIS)); . . public void actionPerformed(ActionEvent ae) { destroyApp(true); notifyDestroyed(); } }
In the code snippet we used Command class. A command is a task that a user can trigger in the application. To assign a command to a menu bar item or device's soft keys we use addCommand() method as shown in the code snippet. In the demo code we use 'Exit' as a command that will trigger an event to close the application. In the code snippet we have used Resource class that is used to access the resources like images, in our case duke.gif. The Resource call needs to be wrapped in try-catch block as it throws IOException error. To add duke.gif image to the resource bundle, add the following to the project's build.xml file so that when the project is compiled the necessary files are added to the bundle and made available for the application.
. .
After compiling and running the project, FirstLWUIT, the Sun Wireless toolkit emulator pops up with the application exceuting the FirstMIDlet where the image and the label are displayed in the center and the buttons are aligned at the botton. Also for left soft key an exit command has been assigned and on clicking that soft key the application ends.
Once the project is created, to add LWUIT.jar to the project resources we have to give the location of the package and add to the Resources.
In Conclusion The Light Weight User Interface Toolkit Library with its Swing-like features enables a J2ME developer to create mobile applications with intuitive interfaces and frees him from the various portability issues.
(For the complete code, go to forums.pcquest.com under the Developers' thread.)
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.