Programming Tutorials

Gauge sample program in J2ME

By: Lakshmi in J2ME Tutorials on 2007-09-16  

A Gauge is a type of user interface component used in J2ME applications to represent a value within a range, such as a percentage or a temperature. It typically consists of a horizontal or vertical bar with a movable pointer or slider that indicates the current value.

Here is a sample program that creates a Gauge with a range from 0 to 10 and an initial value of 5:

import javax.microedition.lcdui.*;

public class GaugeDemo extends MIDlet implements CommandListener {
    private Display display;
    private Form form;
    private Gauge gauge;
    private Command exitCommand;

    public GaugeDemo() {
        display = Display.getDisplay(this);
        form = new Form("Gauge Demo");
        gauge = new Gauge("Value:", true, 10, 5);
        exitCommand = new Command("Exit", Command.EXIT, 0);
        form.append(gauge);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            notifyDestroyed();
        }
    }
}

In this example, the Gauge is created with a label of "Value:", allowing the user to select a value between 0 and 10. The true parameter specifies that the Gauge is interactive, meaning that the user can adjust the value by moving the pointer or slider. The initial value is set to 5. The Gauge is then added to a Form, along with an Exit command that closes the application when pressed.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)