File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes Working with Command Item Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply Bookmark "Working with Command Item" Watch "Working with Command Item" New topic
Author

Working with Command Item

sunil virup
Greenhorn

Joined: Aug 07, 2001
Posts: 5
Hi Folks,
I have recently started working on J2ME and picking up the basics..duirng which I have comeacross the following problem.I have a form with one command button.On click of this command button it takes u to another form and that form has another button..The problem is on click of the button on the second form where does the control go..is it necessary for me to add the commandlistener explicitly..please pitch in with your suggestions....here goes the code that I am executing
TIA
Sunil
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class checkCommands extends MIDlet implements CommandListener
{
Form mainForm = new Form ("StockQuotes");
TextField symbolField = new TextField ("Symbol", "IBM", 5, TextField.ANY);
StringItem resultItem = new StringItem ("", "");
Command getCommand = new Command ("Get", Command.SCREEN, 1);
Display display = null;

public checkCommands()
{
mainForm.append (symbolField);
mainForm.append (resultItem);
mainForm.addCommand (getCommand);
mainForm.setCommandListener (this);
}

public void startApp ()
{
display = Display.getDisplay(this);
display.setCurrent (mainForm);
}

public void pauseApp ()
{ }

public void destroyApp (boolean unconditional)
{ }

public void commandAction (Command c, Displayable d)
{
String cLabel = c.getLabel();
System.out.println("Command :"+c);
System.out.println("Command Label :"+cLabel);
if(cLabel.equals("Get"))
{
Form getForm = new Form("Get Button");
Command cOK = new Command("Bidagain",Command.SCREEN,0);
getForm.addCommand(cOK);
display.setCurrent (getForm);

}
//First show the first form
}

public static void main(String args[])
{
new checkCommands().startApp();
}
}
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
sunil,
I have read your code and below are my sugessions/comments:-
1- it is not a better idea to make a new Form object for every screen rather you should try to reuse the same form object by using delete(int itemno) and set(itemno,newitem) methods of the Form class
2- If you have to make separate Form object then you will have to set the Command Listener for every Form object to which you want to make current screen.
I have made some changes in your code which is given below , may be this is what you want to do
Hope it will help you
Rishi
Code starts here:-
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class CheckCommand extends MIDlet implements CommandListener
{
Form mainForm = new Form ("StockQuotes");
TextField symbolField = new TextField ("Symbol", "IBM", 5, TextField.ANY);
StringItem resultItem;
Command getCommand = new Command ("Get", Command.SCREEN, 1);
Command cOK = new Command("Bidagain",Command.SCREEN,0);
Display display = null;
public CheckCommand()
{
mainForm.append (symbolField);
mainForm.addCommand (getCommand);
mainForm.setCommandListener (this);
}
public void startApp ()
{
display = Display.getDisplay(this);
display.setCurrent (mainForm);
}
public void pauseApp ()
{ }
public void destroyApp (boolean unconditional)
{ }
public void commandAction (Command c, Displayable d)
{
String cLabel = c.getLabel();
System.out.println("Command :"+c);
System.out.println("Command Label :"+cLabel);
if(cLabel.equals("Get"))
{
resultItem=new StringItem("Result",symbolField.getString());
mainForm.set(0,resultItem);
mainForm.removeCommand(getCommand);
mainForm.addCommand(cOK);
}
else
{
symbolField.setString("");
mainForm.set(0,symbolField);
mainForm.removeCommand(cOK);
mainForm.addCommand(getCommand);
}
//First show the first form
}

}
[ August 30, 2002: Message edited by: Rishi Tyagi ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Working with Command Item
 
Similar Threads
IllegalAccessException
NullPointerException
calling .Net web Service by J2ME
Command buttons display on screen
problem with double operations