Gauge sample program in J2ME

By: Lakshmi Viewed: 153289 times  Printer Friendly Format    

The J2ME example shown below places four items on a Form: two gauges and a String label for each one. The labels identify the two different types of gauges defined by the Gauge class: interactive and non-interactive. The implementation renders the two types of gauges differently so the user can distinguish their type.

The user can set the value of an interactive gauge. Simply scroll the display to highlight the first gauge. It takes on a solid rendering when highlighted and is grayed out when not highlighted. Using the left and right arrow buttons that appear at the bottom of the screen, you can modify the gauge's value. Like the vertical arrows you've seen already, these horizontal arrows are created and managed by the implementation.

Notice that you have to click the arrow keys several times before you see an increase or decrease in the number of bars that are filled in. The reason is the limited screen resolution that the gauge is able to display. If the overall range of values that the gauge represents is too great, the gauge implementation must map several values to each vertical bar that appears on the gauge's display. The bottom arrow on the screen indicates that you can scroll down the display further.

gauge. Scrolling the display to the bottom will display the whole of the non-interactive gauge. After you've scrolled down, notice that now there are no left and right arrows, because the value of non-interactive gauges cannot be changed.

It's important to distinguish between the ability to interact with a gauge and the ability to modify its value. Both types of gauges can be modified programmatically.

The abbreviated source code belo shows how to set the maximum and initial values of a Gauge in the constructor.

The four parameters required to specify a gauge are its mode, human readable title, initial value, and maximum value.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
This class demonstrates the use of the Gauge MIDP UI
@see javax.microedition.lcdui.Gauge
public class GaugeDemo extends Form
implements CommandListener
private String gauge1Label = new String("Interactive gauge");
private Gauge interactiveGauge =
new Gauge("Interactive", true, 50, 15);
private String gauge2Label = new String("Non-interactive");
private Gauge staticGauge = new Gauge("Static", false, 50, 25);
public GaugeDemo()
super("Gauge Demo");
instance = this;

Unlike the demo, a real application would presumably also change the gauge value during its lifetime, using the following methods in the Gauge class:

public void setValue(int value)
public int getValue()

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial