November 23, 2014
Hot Topics:

Introducing a Lightweight UI Toolkit: Shake Your User Interface

  • September 25, 2008
  • By Ibon Urrutia
  • Send Email »
  • More Articles »

It is very important that you set the containing form as non-scrollable: List implements its own scroll and your application will behave strangely if you don't do so.

In your application, you pass a Vector of YWRecords to the List, but how is it going to be rendered a YWRecord? They aren't LWUIT components, so you have to tell to the List how to show them. You defined a LocationRenderer class that implements ListInterface:

class LocationRenderer extends Label implements ListCellRenderer {

   public LocationRenderer() {
      getStyle().setPadding(5,5,5,5);
   }

   public Component getListCellRendererComponent(List list,
      Object value, int index, boolean isSelected)
      final YWRecord record = (YWRecord) value;
      setText(record.name);
      if (isSelected) {
         setFocus(true);
         getStyle().setBgImage(resources.getImage("labelback.png"));
         getStyle().setBgTransparency(100);
         getStyle().setFont(resources.getFont("bold"));
      } else {
         setFocus(false);
         getStyle().setBgImage(null);
         getStyle().setBgTransparency(0);
         getStyle().setFont(resources.getFont("small"));
      }

      return this;

      public Component getListFocusComponent(List arg0) {
         setText("");
         setFocus(true);
         getStyle().setBgTransparency(100);
         getStyle().setFont(resources.getFont("big"));
         return this;

      }
   }

ListCellRenderer defines only two methods:

  • getListCellRendererComponent: Receives all values passed to the List, and returns a LWUIT component that displays information as you want. And when you say "component," you are also referring to Containers, so you can see that a List can be composed of complex components.
  • getListFocusComponent: Returns a component that is going to be used to paint the pointer or the current focused component. You can think of it as it was a magnifying glass that you move up or down above the List: It must have some degree of transparency to see what is under it when the focus on the List moves. Once the focused item reaches the cell location, this Component is drawn under the selected item. It can be null, but defining it in your List in conjunction with setting smooth scrolling brings you a cool effect.

In LwuitWeather, you have implemented a common paradigm on LWUIT: You create a component that also implements the ListCellRenderer interface, so it can be returned from those methods. This way, you avoid creating a new Component each time getListCellRendererComponent is called. Remember that we are developing to devices with big memory constraints.

Notice that getListCellRendererComponent also receives all the List as a parameter: With a little coding, it will be easy to create a List that focuses elements and alters the appearance of near components, like in a Fish Eye Menu. I will let that be an exercise for readers to implement it.

Finally, you should react to selections of items on that list. To achieve that, PlacesDisplay will be your actionListener, whose action performing method is shown next:

public void actionPerformed(ActionEvent evt) {
   List selectionList = (List) evt.getSource();
   ...
   YWRecord newSelected = (YWRecord)
      (selectionList).getSelectedItem();

   // We identify when finishes our browsing when we have got
   // a complete Yahoo code
   if (Character.isDigit(newSelected.yahooCode.
       charAt(newSelected.yahooCode.length() - 1))) {
   ...
       midlet.addCityCode(newSelected);
       midlet.setCurrentCity(newSelected);
       midlet.showWeatherForecast();
   } else {
   ...
       createLocationForm(yahooHome + newSelected.url);
   }
}

Figure 4: Location List


Tags: mobility



Page 3 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