Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    160
    Thanks
    40
    Thanked 1 Time in 1 Post

    Question J2ME submenu not listening to command listener

    I am having trouble getting the command listener to listen to my submenu converTemperatureSub(); I have no clue what I am doing wrong as it is the exact same thing as the main menu.. any help is greatly appreciated. Is there a better way to make a submenu then what I am doing?

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.rms.*;
    import java.io.*;
    import java.util.Date;
    
    
    public class UnitConverter extends MIDlet implements CommandListener,
            Runnable {
    
        private Display display = null;
    
        //menu strings
        private static String TemperatureConverter = "Convert Temperature";
        private static String PressureConverter = "Convert Pressure";
        private static String EnergyConverter = "Convert Energy";
        private static String SpeedConverter = "Convert Speed";
        private static String DistanceConverter = "Convert Distance";
    
        //submenu strings
         private static String TempCF = "C to F";
         private static String TempFC = "F to C";
    
    
        //Declaring the menu
        List menu = null;
        Form ui_form = null;
      List menuTemp = null;
    
    
        //Welcome message
        Alert welcome;
        Command Done;
    
        //Declaring Commands
        Command Convert;
        Command Quit;
        Command Back;
    
        String str, res;
        double numUnits;
    
    
    
        StringItem si = null;
        RecordStore recordStore = null;
        // declaring the alert
        Alert timeAlert;
        //  declaring variable ticker
        Ticker ui_ticker = null;
        Image i = null;
    
    
        public UnitConverter()
        {
            display = Display.getDisplay(this);
            Done = new Command("Done", Command.SCREEN, 1);
            Quit = new Command("Quit", Command.SCREEN, 2);
            Convert = new Command("Convert", Command.ITEM, 1);
            Back = new Command("Back", Command.ITEM, 2);
    
        }
        public void startApp()
        {
            Display.getDisplay(this).setCurrent(timeAlert);
            createAlert();
            menu = new List("Term Project - Converter Application", List.IMPLICIT);
             Image  temperature=null;
             Image pression=null;
             Image energy=null;
             Image speed=null;
    
             try {
                temperature = Image.createImage("thermometer.png");
                pression = Image.createImage("pressure.png");
                energy = Image.createImage("energy.png");
                speed = Image.createImage("speed.png");
            } catch (IOException ioe){}
    
            menu.append(TemperatureConverter, temperature );
            menu.append(PressureConverter, pression);
            menu.append(EnergyConverter, energy);
            menu.append(SpeedConverter, speed);
            menu.append(DistanceConverter, null);
    
            // initializing and putting a message in the ui_ticker
            ui_ticker = new Ticker("Welcome to the Converter application 2010");
            // putting the ticker in the menu
            menu.setTicker(ui_ticker);
            menu.addCommand(Quit);
    
            menu.setCommandListener(this);
            display.setCurrent(timeAlert, menu);
        }//End startApp()
    
         public void createAlert() {
            try {
                i = Image.createImage("/ampere-meter.jpg");
            } catch (IOException ioe) {
            }
            // Initializing the alert
            timeAlert = new Alert("Welcome to the Converter application 2010", null, i, AlertType.INFO);
            // setting a string into the alert to display the current date and time
            timeAlert.setString(new Date().toString());
            //  placing a done command to close the alert
            timeAlert.setTimeout(timeAlert.FOREVER);
        }
    
        public void pauseApp()
        {
    
        }//End pauseApp()
    
        public void destroyApp(boolean unconditional)
        {
            notifyDestroyed();
        }//End destroyApp()
    
        public void commandAction(Command c, Displayable d)
        {
            if ( c == Done )
                showMenu();
    
            if ( c == Quit )
                destroyApp(true);
    
    
            if (c == Convert)
                conversion();
    
    
            else if(c == Back)
                display.setCurrent(menu);
    
    
            else if (c == List.SELECT_COMMAND)
                run();
    
    
          
    
    
    
        }//End commandAction
    
        public void run()
        {
            int index = menu.getSelectedIndex();
            if(index == -1) return;
            String command = menu.getString(index);
    
            if (command.equals(TemperatureConverter))
            {
                   customForm("Convert Temperature");
                convertTemperatureSub();
    
            }
    
            else if (command.equals(TempCF))
            {
               customForm("Convert Temperature");
               convertTemperatureCF();
            }
    
             else if (command.equals(TempFC))
            {
     customForm("Convert Pressure");
                convertPressure();
            }
    
            else if (command.equals(PressureConverter)) 
            {
                customForm("Convert Pressure");
                convertPressure();
            }
            else if (command.equals(EnergyConverter)) 
            {
                 customForm("Convert Energy");
                convertEnergy();
            }
            else if (command.equals(SpeedConverter)) 
            {
                 customForm("Convert Speed");
                convertSpeed();
            }
            else if (command.equals(DistanceConverter))
            {
                customForm("Convert Distance");
                convertDistance();
            }
    
        }
    
         public void welcome()
        {
            display = Display.getDisplay(this);
    
            welcome = new Alert("Unit Conversion", "", null, AlertType.INFO);
            welcome.setString("Let's convert some units");
            welcome.setTimeout(Alert.FOREVER);
            welcome.addCommand(Done);
            welcome.setCommandListener(this);
            display.setCurrent(welcome);
        }//END welcome()
    
        public void showMenu()
        {
            menu = new List("Converter", List.IMPLICIT);
    
             Image  temperature=null;
             Image pression=null;
             Image energy=null;
             Image speed=null;
    
             try {
                temperature = Image.createImage("thermometer.png");
                pression = Image.createImage("pressure.png");
                energy = Image.createImage("energy.png");
                speed = Image.createImage("speed.png");
            } catch (IOException ioe){}
    
            menu.append(TemperatureConverter, temperature);
            menu.append(SpeedConverter, pression);
            menu.append(PressureConverter, energy);
            menu.append(EnergyConverter, speed);
            menu.append(DistanceConverter, null);
    
             // initializing and putting a message in the ui_ticker
            ui_ticker = new Ticker("Welcome to the Converter application 2010");
            // putting the ticker in the menu
            menu.setTicker(ui_ticker);
            
    
    
            menu.addCommand(Quit);
    
            menu.setCommandListener(this);
            display.setCurrent(menu);
        }//END showMenu();
    
    
       public void customForm (String fName)
        {
            ui_form = new Form(fName);
    
            ui_form.addCommand(Convert);
            ui_form.addCommand(Back);
    
            ui_form.setCommandListener(this);
            display.setCurrent(ui_form);
        }
    
    
        public void convertPressure()
        {
    
        }
    
        public void convertSpeed()
        {
        }
    
         public void convertTemperatureSub()
        {
    
    
             
             menuTemp = new List("Converter", List.IMPLICIT);
    
             Image temperature=null;
             Image pression=null;
    
    
             try {
                temperature = Image.createImage("thermometer.png");
                pression = Image.createImage("pressure.png");
            } catch (IOException ioe){}
    
            menuTemp.append(TempCF, temperature);
            menuTemp.append(TempFC, pression);
    
            menuTemp.addCommand(Back);
    
            menuTemp.setCommandListener(this);
            display.setCurrent(menuTemp);
         }
    
        public void convertTemperatureCF()
        {
    
            double result;
            TextField num = new TextField("Enter the number of units you would like to convert", "", 50, TextField.ANY);
            ui_form.append(num);
            str = num.getString();
    
            try
            {
                numUnits = Double.parseDouble(str);
            }
            catch (NumberFormatException nfe)
            {
            }
    
            //double fromNum = Double.parseDouble(str);
    
            TemperatureConverter c  = new TemperatureConverter("Celsius", "Fahrenheit", "This is a temp converter",
                            "Temperature converter", "Temp");
            result = c.convertCF(numUnits);
            res = "" + result;
    
    
    
        }//End convertTemp()
    
        public  void convertDistance()
        {
            double result;
    
    
            TextField num = new TextField("Enter the number of units you would like to convert", "", 50, TextField.ANY);
          ui_form.append(num);
            str = num.getString();
    
    
            try
            {
                numUnits = Double.parseDouble(str);
                
            }
            catch (NumberFormatException nfe)
            {
            }
             
    
            //double fromNum = Double.parseDouble(str);
    
            DistanceConverter d  = new DistanceConverter("Kilometers", "Milest", "This is a dist converter",
                            "Distance converter", "Dist");
            result = d.convertKM(numUnits);
            res = "" + result;
    
    
    
        }
    
    
        public void convertEnergy()
        {
    
        }
    
        public void conversion()
        {
             ui_form.append(res);
            //ui_form.append(str);
    
        }
    
    
    }
    Last edited by sackstein; 12-03-2010 at 04:15 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •