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++){
        quit = new Command("Quit", Command.EXIT, 1);
        calculate = new Command("Calculate", Command.SCREEN, 2);

   protected void startApp()throws MIDletStateChangeException{

   protected void pauseApp(){

   protected void destroyApp(boolean unconditional)throws MIDletStateChangeException{

   public void commandAction(Command command, Displayable displayable)

       double dA, dB, dC;
       double total;

            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){
                System.out.println ("Invalid response.  End mileage is less than begin mileage.");
            else if (dB < 0){
                System.out.println ("Invalid response.  End mileage is less than 0.");
            else if (dC == 0){
                System.out.println ("Invalid response.  Gallons equals 0.");
            else if (begin == null){
                System.out.println ("Invalid response.  Begin mileage cannot be blank.");
            else if (end == null){
                System.out.println ("Invalid response. End mileage cannot be blank.");
            else if (gallons == null){
                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;
            if (command == quit){
        catch (MIDletStateChangeException me){
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NSingAuthor Commented:
Thanks!  It worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.