1. Introducing the Lightweight UI Toolkit Library
2. Using Lightweight UI Toolkit Widgets
13. Using Transitions and Animations
The Lightweight UI Toolkit library supports pluggable themes similar to CSS and somewhat simpler than Swing's pluggable Look And Feel.
Every LWUIT component has a style associated with it (see Chapter 8, Using the Style Object). This style can be manipulated manually and can be customized using a set of definitions for a specific component type. For example, in order to make the backgrounds for all the buttons red you can use the following theme:
Button.bgColor=ff0000
This theme sets the background in the style object within the button object to red. A theme can be packaged into a resource file (see Chapter 12, Resources) and it can be loaded or switched in runtime. In order to update a theme after switching you must refresh the root component (the Form/Dialog containing our component tree) using the refreshTheme method to update all styles.
Note - Manually modified style elements are not updated when switching a theme.
For example, if you have a button whose background is customized to blue, and you load or refresh a theme with a different background color for buttons, the new theme affects all button instances except for the one you have modified manually.
This allows you to determine styles for specific components yet still be able to use themes for the general look of the application without worrying about how they affect your changes.
A theme file is very similar in spirit to CSS, yet it is much simpler and it is structured like a Java properties file. A theme file is comprised of key value pairs. The key acts in a similar way to a CSS selector that indicates the component or attribute affected by the theme value. For example:
Button.font – font for all buttons
font – default application font applied to all components where no default is defined
The key element is comprised of an optional unique identifier ID for the component (the UIID) and a required attribute type. Unlike CSS, themes do not support elements such as hierarchy or more complex selectors.
Component UIIDs correspond to the component class name by convention. For example.: Button, Label, CheckBox, RadioButton, Form, etcetera.
The supported attributes and their value syntax are illustrated in Attributes :
|
To install a theme you must load it from the Resources class (see Chapter 12, Resources), from which you receive the already parsed hashtable containing the selectors (keys) and their appropriate values. You then submit this class to the UI manager's setThemeProps method in order to update the current theme. It is a good practice to call refreshTheme on all components in memory (even those that are not visible) otherwise behavior is unpredictable.
While a theme is remarkably powerful and relatively simple, it doesn't allow the deep type of customization some applications require. Developers would often like the ability to control the drawing of all widgets from a single location, relieving them of the need to subclass widgets and manipulate their paint behavior.
LWUIT delegates all drawing to a single abstract base class called LookAndFeel, an instance of which may be obtained from the UIManager. This class has a concrete subclass which provides the default LWUIT look called DefaultLookAndFeel. Both LookAndFeel and DefaultLookAndFeel may be subclassed in order to extend/replace their functionality.
The look and feel class has methods for determining the boundaries (preferred size) of component types and for painting all components. In addition it has some special methods that allow you to bind special logic to components and manually draw widgets such as scroll bars. It is the responsibility of the Look and Feel developer to properly use the Style objects delivered by the theme. If you replace the look and feel class, you must make sure to extract values appropriately from component styles of the theming functionality or LWUIT can break.
For further details about the look and feel classes, please consult the API documentation.