NSing
asked on
How do I return focus to a textfield after and invalid condition is met in Java ME?
How do I return focus to a specific textfield in a form? I have an application that has four textfields, three are for user input and one is for the output. There are 6 invalid responses that will cause an error to be displayed based on either leaving any of the textfields blank, attempting to divide by 0, attempting to divide by a negative number, or attempting to divide a negative number. I would like the focus to be return to the textfield that caused the error. I have tried using display.setCurrent (textfield name), but it won't compile.
Also, I am not getting the messages diplayed correctly when the textfields are null. Only the empty string thread is displayed. Below is the code I have:
import javax.microedition.midlet. *;
import javax.microedition.lcdui.* ;
public class GasMileageCalculator extends MIDlet implements CommandListener{
private Display display;
private Form form;
private TextField beginMileageField;
private TextField endMileageField;
private TextField gallonsField;
private TextField gasMileageField;
final int COUNT = 4;
private Item elements[] = new Item[COUNT];
private String begin, end, gallons;
private Command quit, calculate;
public GasMileageCalculator()
{
display = Display.getDisplay(this);
form = new Form("XXX Gas Mileage");
beginMileageField = new TextField("Enter Begin Mileage:", "", 20, TextField.NUMERIC);
endMileageField = new TextField("Enter End Mileage:", "", 20, TextField.NUMERIC);
gallonsField = new TextField("Enter Gallons:", "", 20, TextField.DECIMAL);
gasMileageField = new TextField("Gas Mileage:", "", 20, TextField.DECIMAL);
elements[0] = beginMileageField;
elements[1] = endMileageField;
elements[2] = gallonsField;
elements[3] = gasMileageField;
for(int itr = 0; itr < COUNT; itr++){
form.append(elements[itr]) ;
}
quit = new Command("Quit", Command.EXIT, 1);
calculate = new Command("Calculate", Command.SCREEN, 2);
form.addCommand(quit);
form.addCommand(calculate) ;
form.setCommandListener(th is);
}
protected void startApp()throws MIDletStateChangeException {
display.setCurrent(form);
}
protected void pauseApp(){
}
protected void destroyApp(boolean unconditional)throws MIDletStateChangeException {
}
public void commandAction(Command command, Displayable displayable)
{
double dA, dB, dC;
double total;
gasMileageField.setString( "");
beginMileageField = (TextField)form.get(0);
begin = beginMileageField.getStrin g();
endMileageField = (TextField)form.get(1);
end = endMileageField.getString( );
gallonsField = (TextField)form.get(2);
gallons = gallonsField.getString();
dA = 0;
dB = 0;
dC = 0;
total = 0;
dA = Double.valueOf(begin).doub leValue();
dB = Double.valueOf(end).double Value();
dC = Double.valueOf(gallons).do ubleValue( );
if (dB < dA){
//display.setCurrent(endMi leageField );
System.out.println ("Invalid response. End mileage is less than begin mileage.");
}
else if (dB < 0){
//display.setCurrent(begin MileageFie ld);
System.out.println ("Invalid response. End mileage is less than 0.");
}
else if (dC == 0){
//display.setCurrent(gallo nsField);
System.out.println ("Invalid response. Gallons equals 0.");
}
else if (begin == null){
//display.setCurrent(begin MileageFie ld);
System.out.println ("Invalid response. Begin mileage cannot be blank.");
}
else if (end == null){
//display.setCurrent(endMi leageField );
System.out.println ("Invalid response. End mileage cannot be blank.");
}
else if (gallons == null){
//display.setCurrent(gallo nsField);
System.out.println ("Invalid response. Gallons cannot be blank.");
}
if (command == calculate){
total = (dB - dA)/dC;
//Format output to two decimal places
double formattedTotal = (double)(int)((total)*100. 0)/100.0;
gasMileageField.setString( String.val ueOf(forma ttedTotal) );
}
try
{
if (command == quit){
destroyApp(true);
notifyDestroyed();
}
}
catch (MIDletStateChangeExceptio n me){
}
}
}
Also, I am not getting the messages diplayed correctly when the textfields are null. Only the empty string thread is displayed. Below is the code I have:
import javax.microedition.midlet.
import javax.microedition.lcdui.*
public class GasMileageCalculator extends MIDlet implements CommandListener{
private Display display;
private Form form;
private TextField beginMileageField;
private TextField endMileageField;
private TextField gallonsField;
private TextField gasMileageField;
final int COUNT = 4;
private Item elements[] = new Item[COUNT];
private String begin, end, gallons;
private Command quit, calculate;
public GasMileageCalculator()
{
display = Display.getDisplay(this);
form = new Form("XXX Gas Mileage");
beginMileageField = new TextField("Enter Begin Mileage:", "", 20, TextField.NUMERIC);
endMileageField = new TextField("Enter End Mileage:", "", 20, TextField.NUMERIC);
gallonsField = new TextField("Enter Gallons:", "", 20, TextField.DECIMAL);
gasMileageField = new TextField("Gas Mileage:", "", 20, TextField.DECIMAL);
elements[0] = beginMileageField;
elements[1] = endMileageField;
elements[2] = gallonsField;
elements[3] = gasMileageField;
for(int itr = 0; itr < COUNT; itr++){
form.append(elements[itr])
}
quit = new Command("Quit", Command.EXIT, 1);
calculate = new Command("Calculate", Command.SCREEN, 2);
form.addCommand(quit);
form.addCommand(calculate)
form.setCommandListener(th
}
protected void startApp()throws MIDletStateChangeException
display.setCurrent(form);
}
protected void pauseApp(){
}
protected void destroyApp(boolean unconditional)throws MIDletStateChangeException
}
public void commandAction(Command command, Displayable displayable)
{
double dA, dB, dC;
double total;
gasMileageField.setString(
beginMileageField = (TextField)form.get(0);
begin = beginMileageField.getStrin
endMileageField = (TextField)form.get(1);
end = endMileageField.getString(
gallonsField = (TextField)form.get(2);
gallons = gallonsField.getString();
dA = 0;
dB = 0;
dC = 0;
total = 0;
dA = Double.valueOf(begin).doub
dB = Double.valueOf(end).double
dC = Double.valueOf(gallons).do
if (dB < dA){
//display.setCurrent(endMi
System.out.println ("Invalid response. End mileage is less than begin mileage.");
}
else if (dB < 0){
//display.setCurrent(begin
System.out.println ("Invalid response. End mileage is less than 0.");
}
else if (dC == 0){
//display.setCurrent(gallo
System.out.println ("Invalid response. Gallons equals 0.");
}
else if (begin == null){
//display.setCurrent(begin
System.out.println ("Invalid response. Begin mileage cannot be blank.");
}
else if (end == null){
//display.setCurrent(endMi
System.out.println ("Invalid response. End mileage cannot be blank.");
}
else if (gallons == null){
//display.setCurrent(gallo
System.out.println ("Invalid response. Gallons cannot be blank.");
}
if (command == calculate){
total = (dB - dA)/dC;
//Format output to two decimal places
double formattedTotal = (double)(int)((total)*100.
gasMileageField.setString(
}
try
{
if (command == quit){
destroyApp(true);
notifyDestroyed();
}
}
catch (MIDletStateChangeExceptio
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER