×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    how to call getDisplay()?

    I have read in the book that getDisplay() can only be called after the beginning of the MIDlet's startApp() method.
    Although I have called getDisplay() in the startApp() , why I cannot call getDisplay as follows.
    Because I cannot call at that place, I have to write getDisplay() whenever I want to show an alert.
    How to solve that problem?

    Code:
    package treadstringrecord;
    
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.midlet.MIDletStateChangeException;
    import javax.microedition.rms.RecordStore;
    import javax.microedition.rms.RecordStoreException;
    import javax.microedition.rms.RecordStoreNotOpenException;
    
    public class Mainformcommandhandler implements CommandListener {
    
    	////////////////Why I could not call getDisplay here///////////////////////////////////////
            //Display display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    	Display display;
    	RecordStore rs;
    	Alert alert;
    
    	public Mainformcommandhandler() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public void commandAction(Command c, Displayable d) {
    		MainForm mainform=(MainForm)d;
    		if(c==mainform.cmdExit)
    		{
    			try {
    				TReadStringRecordMIDlet.treadstringrecordmidlet.destroyApp(true);
    			} catch (MIDletStateChangeException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();/////////////////////////////////see how that is written in the book
    			}
    		}
    		
    		
    		if(c==mainform.cmdOpen)
    		{
    			try {
    				rs=RecordStore.openRecordStore("comment", true);
    			} catch (RecordStoreException e) {
    				// TODO Auto-generated catch block
    				//e.printStackTrace();
    				alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				
    				
    			}
    			
    			String comment1="Excellent";
    			String comment2="very nice";
    			//byte[] readbyte=null;
    			byte[] commentbyte =comment1.getBytes();
    			byte[] commentbyte2=comment2.getBytes();
    			try {
    				rs.addRecord(commentbyte, 0, commentbyte.length);
    				rs.addRecord(commentbyte2, 0, commentbyte2.length);
    			} catch (RecordStoreException e) {
    				// TODO Auto-generated catch block
    				alert=new Alert("Cannot write recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    			}
    			
    			try {
    				StringBuffer buffer=new StringBuffer();
    				for(int i=1;i<=rs.getNumRecords();i++)
    				{					
    					String readrecord=new String(rs.getRecord(i));
    					buffer.append(readrecord);
    				}
    				alert=new Alert("Reading recordstore",buffer.toString(),null,AlertType.INFO);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				alert.setTimeout(Alert.FOREVER);
    			} catch (RecordStoreNotOpenException e) {
    				
    				alert=new Alert("There is no recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    			} catch (RecordStoreException e) {
    				
    				alert=new Alert("error in reading recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				alert.setTimeout(Alert.FOREVER);
    			}
    			
    			try {
    				rs.closeRecordStore();
    			} catch (RecordStoreException e) {
    				
    				e.printStackTrace();
    			}			
    				
    		}//if (c==mainform.cmdOpen)
    		
    		
    		if(c==mainform.cmdDelete)
    		{
    			try {
    				rs=RecordStore.openRecordStore("comment", false);
    			
    			
    			
    			try {
    				int i=rs.getNumRecords();
    				while(i!=0)
    				{
    					rs.deleteRecord(i);
    					i--;
    				}
    			} catch (RecordStoreNotOpenException e) {
    				
    				e.printStackTrace();
    			} catch (RecordStoreException e) {
    				
    				e.printStackTrace();
    			}
    			} catch (RecordStoreException e) {				
    				alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);				
    			}
    		}//if(c==mainform.cmdDelete)
    		
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: how to call getDisplay()?

    There is exactly one instance of Display per MIDlet and the application can get a reference to that instance by calling the getDisplay() method. The application may call the getDisplay() method at any time during course of its execution. The Display object returned by all calls to getDisplay() will remain the same during this time.

    display=Display.getDisplay(Midlet object); should work.

    As a work around you can use Object of midlet say m.display.setCurrent() (make display as public in your midlet class); you don't need to get the instance of display again.
    Thanks,
    Ekta

  3. #3
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: how to call getDisplay()?

    afaik the best place to get a handle (or reference) to the Display object would be in the startApp() method...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: how to call getDisplay()?

    May be your call to the getDisplay(midlet) is not returning anything...why dont you try to paint after the call..?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  5. #5
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: how to call getDisplay()?

    I call as I have shown in the blue part because I don't want to call whenever I try to display the alert. But it does not work.
    Now as ektasrv said , I use the midletname before the setCurrent method as TReadStringRecordMIDlet.treadstringrecordmidlet.display.setCurrent(alert) . It works.
    Now I don't need to getDisplay() many times.

    Thanks u all for ur kind replies.

  6. #6
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: how to call getDisplay()?

    actually u can simply (int he class that extends MIDlet) call that once, place it in a variable which will be re-used :

    private Display myDisplay;
    then in startApp()
    myDisplay = Display.getDisplay(this);

    afterwards you can passt his reference to any other class that needs it... Many times that's implemented through passing it to the constructor of classes that need it...

    this obviously means that you don't have to call getDisplay() many times, which was bad programming practice to begin with...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    113

    Re: how to call getDisplay()?

    Quote Originally Posted by Tiger79 View Post
    actually u can simply (int he class that extends MIDlet) call that once, place it in a variable which will be re-used :

    private Display myDisplay;
    then in startApp()
    myDisplay = Display.getDisplay(this);

    afterwards you can passt his reference to any other class that needs it... Many times that's implemented through passing it to the constructor of classes that need it...

    this obviously means that you don't have to call getDisplay() many times, which was bad programming practice to begin with...
    Ekta has already suggested the same thing and komomo had already resolved his Issue and gave the closing remarks "Thanks u all for ur kind replies." then why to repeat the solution again?
    Regards
    MKS

  8. #8
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: how to call getDisplay()?

    because those are two different solutions

    TReadStringRecordMIDlet.treadstringrecordmidlet.display.setCurrent(alert) . It works.
    Now I don't need to getDisplay() many times.
    My solution actually doesnt involve calling the midlet's class the whole time, and uses a reference to the Display object instead. This is actually a GOOD programming practice because the Display object will be instatiated in one single object (the leading MIDlet class) and will be passed to any using class (btw this was NOT in the other solution) which in turn can use it. It also gives possibilities to other nice things like callbacks, and obviously having the main MIDlet actually "manage" the Display variable instead...

    Sattisfied with the answer ? And did you get up on the wrong foot this morning ?

    To be honest, you should have noticed the difference between the answers yourself before even posting such an reply... I'm guessing you did that because I myself have placed such an reply before, but please read it again and you will notice that I will only ask why an answer is being repeated when it actually is being repeated, and not, as in your case, when actually another option or solution is being adressed...
    Also the fact that a solution has been given does not automatically mean that no other solution and/or thought can be expressed...
    Last edited by Tiger79; 2010-06-18 at 12:26.
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  9. #9
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: how to call getDisplay()?

    Quote Originally Posted by Tiger79 View Post
    this obviously means that you don't have to call getDisplay() many times, which was bad programming practice to begin with...
    As per the API doc:

    javax.microedition.lcdui.Display

    There is exactly one instance of Display per MIDlet and the application can get a reference to that instance by calling the getDisplay() method. The application may call the getDisplay() method at any time during course of its execution. The Display object returned by all calls to getDisplay() will remain the same during this time.

    So if any class of the application does not have the reference of the Midlet's display passed to it, will calling getDisplay( ) to get it again amounts to bad programming practice (in light with the above API doc statement) ?
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  10. #10
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: how to call getDisplay()?

    That was the more elaborate explanation :
    "This is actually a GOOD programming practice because the Display object will be instatiated in one single object (the leading MIDlet class) and will be passed to any using class (btw this was NOT in the other solution) which in turn can use it. It also gives possibilities to other nice things like callbacks, and obviously having the main MIDlet actually "manage" the Display variable instead..."

    It might not actually be a bad programming practice to continuesly call the myPackage.myProject.myMidlet.getDisplay() method, but from an oop point of view to me it sounds futile...
    Otherwise you could simply leave out the whole references implementation as a whole and re-instantiate always new objects to call it's methods... On the other hand this is a singleton so it can be done, but then again having a reference to it's midlet as a whole can make a ton of a difference in terms of functionality (as mentioned callbacks and the sort)...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: how to call getDisplay()?

    then again having a reference to it's midlet as a whole can make a ton of a difference in terms of functionality (as mentioned callbacks and the sort)...
    In the actionhandler or the UIController of your application, you generally keep the reference of the Midlet (as komomo did) to interact display or other midlet life-cycle methods when user wants to exit the application like midlet.destroyApp();

    Should we try to do it in some other better way ? Please suggest.
    Thanks,
    Ekta

  12. #12
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: how to call getDisplay()?

    I already did,

    post #6 instead of post #5

    As far as I could see he wasn't keeping a reference to the Midlet, as can be seen in his code of the Mainformcommandhandler class, where no global reference is set to his Midlet...
    Instead Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    is used, and afterwards, correctly he states :
    "Now I don't need to getDisplay() many times."
    Which also would be the case in the possible solution I gave in post #6...

    Really, it's all there in the posts...

    From that point (post #6) onwards it's only been some kind of argument which one is THE solution... No point in that, cause the first response I gave was simply that this is ANOTHER solution, an alternative... All being developers I guess everyone knows there are more roads that lead to Rome, if someone is so hardheaded he cannot see that then hereby I'd say go on with your solution, I will keep using mine and we all live happily ever after
    Last edited by Tiger79; 2010-06-21 at 13:55.
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: how to call getDisplay()?

    Intend was not to argue, but just to learn something more from your experience. Sorry if it annoyed you.... no more arguments on this topic.
    Thanks,
    Ekta

Similar Threads

  1. Display getDisplay setCurrent Problem
    By kaleith in forum Mobile Java Games
    Replies: 5
    Last Post: 2009-11-03, 15:19
  2. UID of inbox, sent item,missed call, dialed call, received call view
    By soumyadipin in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-02-25, 11:08
  3. Replies: 1
    Last Post: 2008-08-24, 02:06
  4. why doesn't Display.getDisplay(this).vibrate(milis) work on E65?
    By ronikoren in forum Mobile Java General
    Replies: 3
    Last Post: 2008-08-19, 10:36
  5. Display.getDisplay(this)
    By rickong in forum Mobile Java General
    Replies: 0
    Last Post: 2003-05-14, 11:12

Posting Permissions

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