Sunday, April 3, 2011

Java GUI development . LWUIT for J2ME

  • Before  learning to use the LWUIT library to develop mobile phone interfaces, the developer guide advices us to get some background first on the AWT and SWING APIs. So here are some concepts I grasped:

  1. You add components (buttons, check boxes, etc) inside a container (panel / window), and each container can host other containers inside and that ability enables us to create complex interfaces.

  2. Each container has its own Layout Manager, which arranges containers and its components automatically according to the screen resolution, resizing etc, so we don't have to worry about placing our components manually. The default Layout Manger for the main containers is the Border Layout which divides the gui space in five sections: north, south, east, west, and center.

  3. You've got to set up the arrangement and composition of your gui before you display it. (yep, kind of obvious).

  4. For each component you register event handlers to do something according to actions the user perform.

  5. To register such event handlers you need an object that implements the ActionListener interface, or some other listener according to the event source. The issue with interfaces is that you need to implement all the methods defined inside them (those you don't use are left with an empty body. A workaround to that is to use Adapter Classes (or Adapter Interfaces?,mm nevermind) that implement all the interface methods for you and you override only those you're interested in using.

  6. There's another new concept: anonymous classes. Such classes are declared inline and usually contain just a few lines, the lines needed to register the event handler. You can also declare an inner class ( a class inside a class) for those purposes which is the preferred method regarding event handler registration.

  7. The AWT api, creates interfaces that look differently between operating systems because it depends on native components whereas the SWING api provides a consistent look thorough several platforms because it takes care of the drawing by itself.

  • And as its customary when using a new technology, here's my "Hello world" LWUIT application

[caption id="attachment_184" align="aligncenter" width="141" caption="My first LWUIT app"][/caption]

  • The code is practically the same that the one provided inside the developers guide, so there's not reason to post it.

No comments:

Post a Comment