×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    May 2010
    Location
    London
    Posts
    2

    Using the Back Command Correctly

    Hi there, I'm new here, and have just started using J2ME to further my understanding of programming (Even though I have been studying Java for the best part of an academic year now), but have got a little bit stuck writing my first small application (After Hello World of course!), which is why I need some guidance from some far more knowledgable folk!

    I have done quite a bit of searching, and scrolling through tutorials and API's but I'm not sure what I'm doing wrong, so I thought it'd be okay to ask here.

    Basically I've tried to write a Paypal fee calculator, just a really simple application which takes a value from a text box, and when the user hits okay, it performs a small calculation, and then shows what the final amount will be. I know such applications exist (And far more complex than my own), but I wanted to start out simple. I have also tried to include error handling where if a non number is entered it switches to a error message.

    This however is where my problem arises, I'd like to implement a back command from both errorForm and resultForm (See Code) but everything I have tried does not seem to work. I'm not sure if becuase what I have written in the listener is wrong, or I have badly implemented something elsewhere which is causing the problem.

    Also, if there's any other obvious problems or redundencies in my code I'd quite like to know, so I can understand what I'm doing wrong in the future.

    I also have a few other questions which I haven't found a straight answer for.

    1) When using commands, such as ok = new Command("OK", Command.OK, 2); What does the integer refer too? I'm not quite sure how changing this number affects the running of the program. When I played around with it, it didn't seem to have an obvious effect.

    2)Is it necessary to use display = Display.getDisplay(this); at the begginning of methids like I have done? I understand why we do it during startApp(); but the program runs fine with the ones I have commented out.

    Also, I'd rather not just be given code to copy, If possible, an explanation and a sample will do fine, after all I'm trying to make myself a better programmer, not just plagarising someone's code.

    Also, apologies if the questions seem really simple, I have sieved through alot of tutorials, but I can't seem to get it to work. And also Moderators if this is in the wrong place, feel free to move it.

    Many Thanks,

    Michael.

    Code:
    package com.example.michaelapp;
    
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    
    public class MichaelAppMidlet extends MIDlet implements CommandListener{
    	  private Form mainForm, resultForm, errorForm;
    	  private Display display;
    	  private TextField pounds;
    	  private Command ok, exit, back;
    	  
    ////////////////////////////////////////////////////////	
    	
    	  
    	  public MichaelAppMidlet(){
    	    pounds = new TextField("(£) Pounds: ", "0.00", 10, TextField.ANY);
    		exit = new Command("Exit", Command.EXIT, 1);	    
    	    ok = new Command("OK", Command.OK, 2);
    	    back = new Command("Back", Command.BACK, 1);    
    	  }
    	  
    ////////////////////////////////////////////////////////	  
    
    	  public void startApp(){
    	    display = Display.getDisplay(this);
    	    Form mainForm = new Form("PayPal Calculator");
    	    mainForm.append(pounds);
    	    mainForm.addCommand(ok);
    	    mainForm.addCommand(exit);
    	    mainForm.setCommandListener(this);	    
    	    display.setCurrent(mainForm);
    	  }
    
    	  public void pauseApp(){
    	  
    	  }
    
    	  public void destroyApp(boolean destroy){
    	    notifyDestroyed();
    	  }
    	  
    ////////////////////////////////////////////////////////
    
    	  public void showInput(){	  
    		 		  
    	    //display = Display.getDisplay(this);
    	    String input = pounds.getString();
    	    
    	    try{
    	       double num = Double.valueOf(input.trim()).doubleValue();	   //Parse Input
    	       double midVal = (num*0.034); 
    	       double longVal = (num-midVal)- 0.20;	  
    	       int x = (int)(longVal * 100.0);	    
    	       double finalVal = ((double)x)/100.0;	    
    	       
    	       Form resultForm = new Form("Your Fees");
    	       resultForm.append("You final amount after fees is £" + finalVal + ". ");
    	       resultForm.addCommand(back);
    	       resultForm.setCommandListener(this);	
    	       display.setCurrent(resultForm);
    	    
    	    }catch(NumberFormatException e){
                 error();
    		 }
    	  }
    	  
    ////////////////////////////////////////////////////////
    
    	  public void error(){
    		    //display = Display.getDisplay(this); 
    	    	Form errorForm = new Form("Error");
    	    	errorForm.append("You have entered an invalid number. Try making sure " +
    		    		"you are inputing a number greater than zero, or perhaps you have " +
    		    		"accidentally entered a letter");
    		    errorForm.addCommand(back);
    		    errorForm.setCommandListener(this);
    		    display.setCurrent(errorForm);
    	  }	  
    	  
    ////////////////////////////////////////////////////////
    	  
    	  public void commandAction(Command c, Displayable d) {
    	    
    	    if(c == ok){
    	      showInput();
    	    } 
    	    else if (c == back){
    	    	 display.setCurrent(mainForm);
    	    } 
    	    else if (c == exit){
    	        destroyApp(false);
    	    }
    	  }
    	}

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Using the Back Command Correctly

    Hello MRLee,

    and welcome to Forum Nokia Discussion Boards!

    First, the back command issue is due to this in your code:

    ..
    private Form mainForm, resultForm, errorForm;
    ..

    Form mainForm = new Form("PayPal Calculator");
    ..


    Instead of redefining the mainForm as a local variable in startApp(), try the following:


    private Form mainForm, resultForm, errorForm;
    ..

    mainForm = new Form("PayPal Calculator");
    ..


    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  3. #3
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Using the Back Command Correctly

    Regarding:

    1) When using commands, such as ok = new Command("OK", Command.OK, 2); What does the integer refer too? I'm not quite sure how changing this number affects the running of the program. When I played around with it, it didn't seem to have an obvious effect.
    The integer is priority value for a Command which, together with Command type, is used to determine the place of the Command on UI. The order of Commands of the same type is handled with the priority value. A Command that has been assigned the smaller priority value is placed before the other Commands of the same type in a menu.

    More about Commands:

    Java Developer's Library 3.5 > Developer's Guides > UI and graphics > LCDUI > Using the LCDUI > Command

    2)Is it necessary to use display = Display.getDisplay(this); at the begginning of methids like I have done? I understand why we do it during startApp(); but the program runs fine with the ones I have commented out.
    Based on your description, you are most likely running the application on some SDK emulator, correct?

    For actual devices, Display instance returned by Display.getDisplay (MIDlet midlet) is required as defined by MIDP, otherwise the application does not show Displayable i.e. the UI elements on the screen.

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    London
    Posts
    2

    Re: Using the Back Command Correctly

    r2j7 Thank you very much, I didn't even notice I had done that! I'm quite prone to silly mistakes, and it usually takes a second pair of eyes for it to be noticed ;p

    I thought that may have been the case, for prioritising, but since I only had one command (back) on the resultForm and errorForm, I didn't really notice the change.

    And correct, I've been running it on the defaultcolourphone while using Eclipse, although I have partially tested it on my 5800 handset.

    Once again, many thanks!

Similar Threads

  1. Problem in Going Back from Form to List
    By lopesrohan1988 in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-07-07, 08:11
  2. Go back to caller view...
    By VinodRaut in forum Symbian User Interface
    Replies: 4
    Last Post: 2007-05-11, 14:22
  3. Errors building project in VS.2003
    By jensesaat in forum Symbian
    Replies: 11
    Last Post: 2006-11-13, 16:54

Posting Permissions

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