aspose file tools*
The moose likes Java Micro Edition and the fly likes Working with Command Item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
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 ]
 
Consider Paul's rocket mass heater.
 
subject: Working with Command Item