The open source framework for developing user interfaces in J2ME

SourceForge.net Logo
Tutorial
Learning MWT is easy and fast, during the next examples you will learn all you need to use the framework.
Source code is written using educational syntaxis and targeting the use of the framework. This means that some pieces of code may be not optimized.
You will need some previous knowledge of J2ME and a UI framework like AWT, SWT or Swing.

Setting Up
First, you need to include the mwt.jar size into your project's library folder.
If you are using an IDE like Eclipse, you must add the mwt.jar as a library.
In Eclipse go to your project's properties → Java Build Path → Libraries → Add JARs... and add the mwt.jar.
Read you IDE's documentation on how to do it.

When you're building your project with Ant and Antenna extensions, you must set the mwt.jar path when compiling and packaging:

	<wtkbuild srcdir="..." destdir="..." classpath="lib/mwt.jar"/>

	<wtkpackage jarfile="..." jadfile="...">
		<libclasspath path="lib/mwt.jar"/>
	</wtkpackage>


1 - Hello World
Source - Jad - Jar - Java WebStart
Let's begin with the typical hello world application.
Here you will learn the minimal stuff necessary to use the framework; instanciate a Window, notify inputs and paint it.
Press the KEY_NUM5 to exit.

2 - Click Counter
Source - Jad - Jar - Java WebStart
This example introduces focus movement and buttons actions.
The Canvas class implements the EventListener interface, so it can handle MWT events.
In this example, the resulting event is determined by the sender component. However, this is a bad practice. In the next example you will see a better EventListener implementation.
Use KEY_NUM2/8/6/4 to move focus between buttons.

3 - Plug In
Source - Jad - Jar - Java WebStart
Here you can see how MWT works friendly with your application.
The example simulates a game. When the KEY_NUM0 is pressed a menu is shown.
As you can see, the application is still running normally in "background".
The EventListener implementation is more elegant; it checks the event type and the button's action event to determine which action will be processed.
You can resume the game or exit pressing KEY_NUM5.

4 - Key States
Source - Jad - Jar - Java WebStart
You can get a key state using your window object, even for things that has nothing to do with MWT.
This example is like the example 3, except it uses the getKeyState method instead of saving flags.
Remember, this is optional. You don't need to use the window's key state buffer if you won't.

5 - Look and Feel
Source - Jad - Jar - Java WebStart
This introduces some custom look and feel over MWT components.
It shows how to extend the Skin class also.
The point here is to see how MWT works, nice results depends on you.

6 - Dynamic Properties
Source - Jad - Jar - Java WebStart
The example shows how properties can be changed in runtime.
Very simple, but you will get the idea of how MWT components works.

7 - Dialogs
Source - Jad - Jar - Java WebStart
Now the main window will work with dialogs.
This may look more complex than previous examples. Be sure to have understood thoroughly the previous examples before continuing.

8 - Custom Window
Source - Jad - Jar - Java WebStart
Here you can see how to extend a window to change its behavior.
The getFocusAction method is overriden, now you can use action keys instead of the KEY_NUMs to move the focus.
The dispatchKey is overriden also, the focus is moved only when the key is released.

9 - Scroller
Source - Jad - Jar - Java WebStart
This example creates a new container component that can scroll its child components.
You can see how it overrides the add and remove methods to prevent removing the internal button that activates the scroll.
Move the focus with KEY_NUMs keys until the small grey square becomes dark green. Then press KEY_NUM5 to activate the scroller movement, (the green square becomes light green).
Use KEY_NUM4/6/2/8 to scroll over the component content and find the exit button.
Be advised, a real life scroller must be implemented in a more efficient way. This is not this tutorial objective.

10 - Double Buffering
Source - Jad - Jar - Java WebStart
This is a very important example because it shows how to improve performance using different techniques.
Press KEY_NUM2/8/6/4 to move the window and KEY_NUM1/9/7/9 to resize it.
As you can see, as the window gets bigger, the program runs slower.
This is because in order to fill the window area, the skin's image are repeated, and this slows the program's performance.
A way to improve this issue is resizing the skin's images when it is created.
Press the KEY_POUND and KEY_START to stretch or shrink images.
As you can see as the images get bigger, the program runs at a better rate. However, this consumes more memory, and since images are resized, they lose transparency. (you can write your own resizing routine instead of using the Skin's one).
Another way to improve performance is double buffering the window. (press KEY_NUM5)
This improves performance significally, but the default Skin's buffering method doesn't process alpha values.
You can change the skin buffering mechanism overriding the createBuffer method.

11 - Custom Fonts ~ MF
Source - Jad - Jar - Java WebStart
This example shows how to use custom fonts for your applications.
Check Font class javadocs and Font documentation for more information.
NOTE: MF Fonts are deprecated

12 - Custom Fonts ~ Mapped
Source - Jad - Jar - Java WebStart
Like example 11, using Mapped fonts instead.

13 - Custom Fonts ~ Stripped
Source - Jad - Jar - Java WebStart
Like example 11, using Mapped fonts instead.

14 - TextBox in Canvas
Source - Jad - Jar - Java WebStart
A simple low-level custom textbox.

Copyright © 2005-2007 J2ME-MWT Team. All Rights Reserved.
Java, J2ME, MIDP and CLDC are Trademarks/Copyright © 1995-2007 Sun Microsystems, Inc.