×

Discussion Board

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

    Question Could not display a runnable Alert after another displayable

    Hi all. I'm trying to do a YesNo dialog for JavaME. I call this dialog in "startApp()" as first displayable and it comes. But if I call it after another displayable(some forms) or in a displayable, the YesNo dialog doesn't come. Please help me. Thanks in advance.

    YesNo.java
    PHP Code:
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;

    public class 
    YesNo extends Alert implements CommandListenerRunnable {

        private 
    boolean result;
        private 
    boolean done false;

        public 
    YesNo(String titleString text) {
            
    super(titletextnullAlertType.WARNING);
            
    setTimeout(FOREVER);
            
    addCommand(new Command("Yes"Command.OK0));
            
    addCommand(new Command("No"Command.OK1));
            
    setCommandListener(this);
        }
        public 
    void commandAction(Command cDisplayable d) {
            
    String s c.getLabel();
            if (
    s.equals("Yes")) {
                
    result true;
            } else if (
    s.equals("No")) {
                
    result false;
            }
            
    done true;
        }
        public 
    void run() {
            try {
                while (!
    done) {
                    
    System.out.println("running");
                    
    Thread.sleep(1000);

                }
            } catch (
    Exception e) {
            }
        }
        public 
    boolean getResult() {
            
    run();
            return 
    result;
        }

    MyMidlet.java
    PHP Code:
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.midlet.*;
    import screens.YesNo;

    public class 
    MyMidlet extends MIDlet {

        public 
    void startApp() {
            
    // this "if block" works alone
            
    if (showYesNo("Warning""Do you want to delete this item?")) {
                
    System.out.println("item's deleted!");
            }
            else
                
    System.out.println("item's NOT deleted!");
            
    // but the same "if block", in the form of commandAction method, is not coming on display.
            
    showAForm();
        }

        public 
    void pauseApp() {
        }

        public 
    void destroyApp(boolean unconditional) {
        }

        public 
    void showAForm() {
            
    Form f = new Form("Form");
            
    f.addCommand(new Command("Call YesNo"Command.SCREEN0));
            
    f.setCommandListener(new CommandListener() {

                public 
    void commandAction(Command cDisplayable d) {
                    
    // if this "if block" calls the YesNo, it can run as you can see at output console, but it is not coming on display.
                    
    if (showYesNo("Warning""Do you want to delete this item?")) {
                        
    System.out.println("item's deleted!");
                    }
                }
            });
            
    Display.getDisplay(this).setCurrent(f);
        }

        public 
    boolean showYesNo(String titleString text) {
            
    YesNo yesno = new YesNo(titletext);
            
    Display.getDisplay(this).setCurrent(yesno);
            return 
    yesno.getResult();
        }


  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Could not display a runnable Alert after another displayable

    When you call Display.setCurrent() in an event handler method (like commandAction()), the display won't change until the event method completes. In your code, you block the event thread, preventing the commandAction() method from exiting. By blocking the event thread, you stop the events (that would paint the alert) from being delivered. The event thread can't deliver two events at once, and it's already delivering a commandAction event.

    Don't try to create a modal alert. Just display the alery, and use the commandAction() event from the alert to trigger the delete.

    Graham.

Similar Threads

  1. Alert with both Yes & No Commands
    By g.castaldi in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2012-12-14, 06:44
  2. Start Midlet automatically using PushRegistry.RegisterAlarm
    By henixy2k2 in forum Mobile Java General
    Replies: 4
    Last Post: 2009-09-01, 20:06
  3. Series 60 J2ME emulator problems
    By evertomi in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2009-06-15, 10:21
  4. consecutive alert display
    By prashantpersistent in forum Mobile Java General
    Replies: 1
    Last Post: 2008-08-12, 10:13
  5. Alert won't let another Displayable be garbage collected
    By lprosek in forum Mobile Java General
    Replies: 3
    Last Post: 2003-08-19, 23:08

Posting Permissions

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