November 26, 2014
Hot Topics:

Introducing a Lightweight UI Toolkit: Shake Your User Interface

  • September 25, 2008
  • 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());
   locList.setSmoothScrolling(true);
   locList.setFixedSelection(List.FIXED_CENTER);
   locList.addActionListener(this);

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

   if (page.navigationLetters != null) {
      List navList = new List(page.navigationLetters);
      navList.setOrientation(List.HORIZONTAL);
      navList.setBorderGap(1);
      navList.setListCellRenderer(new LocationRenderer());
      navList.setSmoothScrolling(true);
      navList.setFixedSelection(List.FIXED_CENTER);
      navList.setSelectedIndex(selectedIndex);
      navList.addActionListener(this);
      locContainer.addComponent(BorderLayout.NORTH, navList);
   }

   form.setScrollable(false);
   form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
   form.addComponent(locContainer);
}

Tags: mobility



Page 2 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel