November 27, 2014
Hot Topics:

Introducing a Lightweight UI Toolkit: Bringing Desktop Development into Java ME

  • July 17, 2008
  • By Ibon Urrutia
  • Send Email »
  • More Articles »

Loading a theme and resources

After defining your theme, adding your images, and setting your fonts, your app needs to load all this information. I put the resources file on the default package in the src directory, to be packed easily in the jar by Netbeans, but you can modify the proportioned ant script to satisfy your needs. To load your theme, you have to open your resource file, set your theme on UIManager (Singleton class that manages the look of the application), and at least, if you are using WTK as an emulator, refresh the theme on Display. I didn't find any reference in the LWUIT documentation but until I had refreshed my theme, my changes weren't shown. If you plan to change themes at runtime, you also have to refresh it. In the following three lines of code, the created theme is applied to the weather app:

res = Resources.open("/weather.res");
UIManager.getInstance().setThemeProps(res.getTheme
   (res.getThemeResourceNames()[0]));
...
//Without doing this, the theme is not shown
Display.getInstance().getCurrent().refreshTheme();

To load images from the resource file, you simply call the getImage method, and to change their size, call scaling methods, as shown here.

Label icon =
   new Label(resources.getImage(weatherRss.actualCondition.code +
             ".png").scaled(40, 40));
When you select your obfuscating level on project properties, don't set it very high or calls to Image.scaled() will fail, exiting your application. For example, I selected a level of 4.

In the WeatherDisplay class, I show a scaled image for the "actual condition," and I use the normal size image for "forecast." And here you have final result:

Figure 9: A more polished screen

Figure 10: Good read on small screens


Tags: mobility



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