Learning Light Weight User Interface Toolkit(LWUIT)

2 comments
Learning Light Weight  User Interface Toolkit(LWUIT) for programming with  Java Me Microedtion(J2ME)
Getting started with Lwuit.



Hello World Example for MIDP
This is a simple hello world example written on top of MIDP. All UI code making
use of the Lightweight UI Toolkit is compatible to other platforms such as CDC.1
1. As of this writing the CDC version of LWUIT required for this compatibility hasn't been released to the
public.
Chapter 1 Introducing the Lightweight UI Toolkit Library 1-3
However, this example is specifically for MIDP. For MIDP the application
management system (AMS) requires a MIDlet class to exist, where in a CDC
environment an Xlet would be expected (and in Java SE you would expect a main
class, and so forth).


import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class HelloMidlet extends javax.microedition.midlet.MIDlet {
public void startApp() {
//init the LWUIT Display
Display.init(this);
// Setting the application theme is discussed
// later in the theme chapter and the resources chapter
try {
Resources r = Resources.open("/myresources.res");
UIManager.getInstance().setThemeProps(r.getTheme(
r.getThemeResourceNames()[0])
);
} catch (java.io.IOException e) {
}
Form f = new Form();
f.setTitle("Hello World");
f.setLayout(new BorderLayout());
f.addComponent("Center", new Label("I am a Label"));
f.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

This example displays the text Hello World on the emulators screen when the program is run.



Notice in this example that the very first line of code for any application using
the Lightweight UI Toolkit library must register the main class with the display. This
behavior is tool-specific. In MIDP there is not much you can do without a reference
to the parent MIDlet, so this operation must be performed in the beginning of the
application.
The creation of the UI code is left within the MIDlet for simplicity but it could be
separated to any class to allow full portability in any future platform to which the
Lightweight UI Toolkit library would be ported.

2 comments: