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(this);
   }

   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.getString();
       
            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).doubleValue();
            dB = Double.valueOf(end).doubleValue();
            dC = Double.valueOf(gallons).doubleValue();
           
            if (dB < dA){
                //display.setCurrent(endMileageField);
                System.out.println ("Invalid response.  End mileage is less than begin mileage.");
            }
           
            else if (dB < 0){
                //display.setCurrent(beginMileageField);
                System.out.println ("Invalid response.  End mileage is less than 0.");
            }
           
            else if (dC == 0){
                //display.setCurrent(gallonsField);
                System.out.println ("Invalid response.  Gallons equals 0.");
            }
           
            else if (begin == null){
                //display.setCurrent(beginMileageField);
                System.out.println ("Invalid response.  Begin mileage cannot be blank.");
            }
           
            else if (end == null){
                //display.setCurrent(endMileageField);
                System.out.println ("Invalid response. End mileage cannot be blank.");
            }
           
            else if (gallons == null){
                //display.setCurrent(gallonsField);
                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.valueOf(formattedTotal));
        }
        try
        {
            if (command == quit){
                destroyApp(true);
                notifyDestroyed();
            }
        }
        catch (MIDletStateChangeException me){
        }
   }
}
NSingAsked:
Who is Participating?
 
sailingbyeCommented:
Have you tried display.setCurrentItem (textfield_object) ?

It isn't guaranteed to give focus to the textfield but will do if it can.

Hope this helps.
0
 
NSingAuthor Commented:
Thanks!  It worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.