March 5, 2021
Hot Topics:

Introducing a Lightweight UI Toolkit: Shake Your User Interface

  • By Ibon Urrutia
  • Send Email »
  • More Articles »

Figure 2: Second page showing states

Figure 3: Last page where links have city codes

The PlacesDisplay class works in a loop, when some element is selected in one of the first lists (regions, countries) or in the alphabetical menu; it downloads and parses all information of that element's link page. You will know that you get a city code because all city codes end with a digit.

And, you can implement all this behavior by using the features provided by one of the most interesting components of LWUIT: Lists.

What is a List?

A List is simply a group of elements displayed in a single column. Explained in that way,- it seems like a very basic feature, but the key to that definition is the word "elements." A List is composed of anything you can imagine, from Containers to simple Labels, and moreover, LWUIT offers a way to define how that list is going to be rendered, a MVC architecture, and a very elegant way of working with selection events.

You can create menus by using buttons, as you did in the first article of this series, but if you use Lists, you encounter some benefits:

  • As discussed earlier, you only can change all buttons' appearance with the Resource Editor. It forces you to uses Styles and apply them to every component you want to be different. With Lists, you define the exact appearance your components will have, so you can set a Label style on your resources file, and override its appearance for all List elements.
  • You can define different button states, but a button can contain only an Image and/or text. A List can be composed of any component—for example, Container elements—so you can have a completely different appearance when an element is selected.
  • With buttons, you have to define CommandListeners for every one. Okay, you can define only one, but you have to discriminate which button was selected; that sometimes generates ugly code. With Lists, you set only one List listener; it receives what component was selected.

Lists also allow you to organize your code better because it follows the Model-View-Controller pattern:

  • The List object itself is the controller: It accepts the user's inputs and modifies the model state.
  • The ListCellRenderer class defines how that List is going to be drawn onscreen.
  • The ListModel class is the model: It will be asked to return only visible elements, so it can be used to keep in memory only the indispensable information. It can act as a mirror of remote data, for example.

Your Location List example

To create a List, you can pass to the constructor a Vector, an array of objects, or a ListModel object. In the following listing, you create and initialize your location list. Notice that you only create a horizontal list of letters when the parsed page has those links:

private void initList(ParsedWeatherPage page, Form form) {
   List locList = new List(page.locations);
   locList.setListCellRenderer(new LocationRenderer());

   Container locContainer = new Container(new BorderLayout());
   locContainer.addComponent(BorderLayout.CENTER, locList);

   if (page.navigationLetters != null) {
      List navList = new List(page.navigationLetters);
      navList.setListCellRenderer(new LocationRenderer());
      locContainer.addComponent(BorderLayout.NORTH, navList);

   form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

Page 2 of 7

This article was originally published on September 25, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date