Monday, August 4, 2008

Using Interactive Gause in J2ME application [Source Code]

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;

public class GaugeExample extends MIDlet implements CommandListener{
private Display display;
private Form fmMain;
private Command cmExit;
private Command cmStop;
private Gauge gaProgress;
private Timer tm;
private GuageTimerTask tt;

public GaugeExample() {
display = Display.getDisplay(this);

gaProgress = new Gauge("Gauge Progress", false, 20, 1);
cmExit = new Command("Exit", Command.EXIT, 1);
cmStop = new Command("Stop", Command.STOP, 1);

fmMain = new Form("");
fmMain.append(gaProgress);
fmMain.addCommand(cmStop);
fmMain.setCommandListener(this);
}

public void startApp() {
display.setCurrent(fmMain);
tm = new Timer();
tt = new GuageTimerTask();
tm.scheduleAtFixedRate(tt, 0, 500);
}

public void pauseApp(){ }

public void destroyApp(boolean unconditional) { }

public void commandAction(Command c, Displayable s) {
if (c == cmExit){
destroyApp(false);
notifyDestroyed();
}else if (c == cmStop){
tm.cancel();
fmMain.removeCommand(cmStop);
fmMain.addCommand(cmExit);
gaProgress.setLabel("Progress Cancelled!");
}
}

/*--------------------------------------------------
* Inner Class
*-------------------------------------------------*/
private class GuageTimerTask extends TimerTask {
public final void run() {
if (gaProgress.getValue() < gaProgress.getMaxValue())
gaProgress.setValue(gaProgress.getValue() + 1);
else {
fmMain.removeCommand(cmStop);
fmMain.addCommand(cmExit);
gaProgress.setLabel("Guage Complete!");
cancel();
}
}
}
}

No comments:

search engine

Custom Search