This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with CommandListener -- IS IT A BUG  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ,
can u pls see the following code n tell me if therz any wrong usage of CommandListener .
As the System out i have given inside the CommandAction is not getting printed on the console untill i close the emulator completely .
My IDE : FORTE FOR JAVA RELEASE 2.0 COMMUNITY EDITION integrated with j2me1.0.2 .
If i click on the "exit" button n times it stays the same ( doesnt respond ) untill i close the emulator and then i prints the System out n times..
Is there any specific problem or specific way in which i have to use the CommandListener .
This looks pretty ordinary but is creating a lot of problem for me in my development of an embedded application.
Can u pls let me know abt the solution for this as soon as possible..
NOTE : I have commented some of the lines and also methods to make it the simplest MIDLET to check for CommandAction just to make sure that itz not going wrong elsewhere..
Thanq in advance
package TestCommands;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
* @author sjatla
* @version
public class TestCommands extends MIDlet implements CommandListener {
private Command exitCommand; // The exit command
private Display display; // The display for this MIDlet
public TestCommands() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
public void startApp() {
TextBox t = new TextBox("Hello MIDlet", "Test string", 256, 0);
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
public void pauseApp() {
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
public void destroyApp(boolean unconditional) {

private void downloadPage(String url) throws IOException {
/* StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;
System.out.println("the url is " + url);
try {
long len = 0 ;
int ch = 0;

c = (HttpConnection);
System.out.println("the connec is " + c);
is = c.openInputStream();
System.out.println("before c.getLength() & is is " + is);
len =c.getLength() ;
System.out.println("the length is " + len);
if ( len != -1) {
// Read exactly Content-Length bytes
for (int i =0 ; i < len ; i++ )
if ((ch = != -1)
b.append((char) ch);
} else {
// Read till the connection is closed.
while ((ch = != -1) {
len = is.available() ;
t = new TextBox("hello again....", b.toString(), 1024, 0);
} catch (Exception e)
System.out.println("Exception in downloadPage is "+ e);
finally {
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
System.out.println("am in exitCommand");
}catch(Exception e){e.printStackTrace();}
thanx & regards
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!