开发者

Display alert and then jump to another form in j2me

开发者 https://www.devze.com 2023-04-10 16:19 出处:网络
I am working on J2ME application. I want to show alert in Form and display another Form from another class. I have tried the following method to show alert开发者_StackOverflow中文版.

I am working on J2ME application. I want to show alert in Form and display another Form from another class. I have tried the following method to show alert开发者_StackOverflow中文版.

public void showMsg() 
    {
        Alert success = new Alert("Data Not found.");
        //success.setImage(img2);
       success.addCommand(new Command("Ok", Command.OK, 0));
       success.addCommand(new Command("Cancel", Command.CANCEL, 0));
       success.setCommandListener(this);
       success.setTimeout(Alert.FOREVER);

        Display.getDisplay(parent).setCurrent(success, chapterForm);
    } 

After showing the alert I am jumping to another form as:

Display.getDisplay(parent).setCurrent(welcomeForm);

When I run this it don't show the alert but jump to the welComeForm. So, how can I show alert and then jump to another form.


The Alert won't advance automatically to chapterForm because you have replaced the default listener on the Alert with this. Use the commandAction() event in the CommandListener interface to get the OK or Cancel from the Alert. Then you can use Display.setCurrent(Displayable d) to show the Form you want to display.


Display.getDisplay(parent).setCurrent(welcomeForm) is most likely the reason why it don't show the alert but jump to the welComeForm. To be precise it (device) may show alert for a moment, but as soon as you invoke that setCurrent(welcomeForm), it gets momentarily overwritten by welcomeForm.

If you want welcomeForm to be dissplayed by command from alert, just

  1. wipe out the code setCurrent(welcomeForm) from where it is now

  2. insert that wiped-out code into this.commandAction method (this is command listener you use in your code exerpt)


A nifty solution is to start a new thread after setting the current display to the Alert, and in this new thread you can do a Thread.sleep(2000); in order to wait, and after that you display the new form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号