×

Discussion Board

Results 1 to 2 of 2

Thread: GUI delay bug

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    9

    GUI delay bug

    Hi,

    i have start to try out lwuit.

    I have build 3 screens. When ein push a button cmdSlide, i want show the 2. screen for a certain time (2 sec) and slide without to push a button to the 3. screen.

    But with this code it is not possible. With Thread.sleep(2000) 2 sec wait on the 1. screen, not on the 2. screen. The 2 sec comes everytime with the button-event.
    Anyone an idea how i could solve this delay problem to get a delay on the 2. screen.

    Thx

    import java.io.IOException;

    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    import com.sun.lwuit.Button;
    import com.sun.lwuit.Command;
    import com.sun.lwuit.Container;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.animations.CommonTransitions;
    import com.sun.lwuit.animations.Transition3D;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.layouts.BoxLayout;
    import com.sun.lwuit.plaf.UIManager;
    import com.sun.lwuit.util.Resources;

    public class BasicForm extends MIDlet implements ActionListener {

    private Form form1;
    private Form form2;
    private Form form3;

    //private Command cmdRotate = new Command("Rotate");
    private Command cmdSlide = new Command("Slide");
    private Command cmdSlide2 = new Command("Slide2");
    private Command cmdSlide3 = new Command("Slide3");


    private Command cmdExit = new Command("Exit");

    public BasicForm() {
    //Initialize the LWUIT
    Display.init(this);
    }

    protected void startApp() throws MIDletStateChangeException {
    //Load the Theme
    Resources r;
    try {
    r = Resources.open("/LWUITtheme.res");
    UIManager.getInstance().
    setThemeProps(r.getTheme("LWUITDefault"));
    } catch (IOException e) {
    e.printStackTrace();
    }


    form1 = new Form("Form 1");
    form1.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    form1.addComponent(0,null,new Label("This is the first Form"));
    form1.addCommand(cmdSlide);
    form1.addCommand(cmdExit);
    form1.addCommandListener(this);
    form1.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 1000));



    //Setup Form 2
    form2 = new Form("Form 2");
    form2.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    form2.addComponent(0,null,new Label("This is the second Form"));
    //form2.addCommand(cmdSlide2);
    //form2.addCommand(cmdExit);
    form2.addCommandListener(this);
    form2.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 1000));



    form3 = new Form("Form 3");
    form3.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    form3.addComponent(0,null,new Label("This is the third Form"));
    form3.addCommand(cmdSlide3);
    form3.addCommand(cmdExit);
    form3.addCommandListener(this);
    form3.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 1000));


    form1.show();
    }

    public void actionPerformed(ActionEvent evt) {
    //check which command cliked
    if (evt.getCommand()==cmdExit) {
    notifyDestroyed();
    } else if (evt.getCommand()==cmdSlide) {
    form2.show();
    try {
    Thread.sleep(2500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    form3.show();

    }else if (evt.getCommand()==cmdSlide3) {
    form1.show();
    }
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    }

  2. #2
    Regular Contributor
    Join Date
    Nov 2008
    Posts
    75

    Re: GUI delay bug

    Hi,

    The problem you are facing is one all beginners face before too long, and even experienced Java developers re-encounter from time to time. When your user interacts with the GUI (e.g. clicks a button), your actionPerformed method gets invoked, but it is being invoked on the the System event thread. You must not tie up this thread, because if you do the system will be unresponsive. By "unresponsive" I mean it will not respond to additional user actions, and it will not update the display.

    In your program the actionPerformed method is trying to update the display and then sleep for SECONDS (an eternity), ALL on the system event thread. The GUI cannot be updated during the time you are "hogging" that thread.

    if you want to do any work beyond the most minor bookkeeping you must do it in a different thread. You can create a new thread to do the work, or you can send a messages to an already-running thread to do the work, but if you don't do it in a different thread then... well, you've seen what happens.

    If you don't know about threading then you need to find something to read. It's not a short or simple subject, and certainly can't be explained in a discussion group.

    Good luck.
    Cheers,

    Matt Brenner
    UnME2, Inc.

Similar Threads

  1. Replies: 8
    Last Post: 2009-03-31, 09:18
  2. S60V3 - showing GUI from a non GUI exe
    By tal.shahar@mobile-mx.com in forum Symbian
    Replies: 4
    Last Post: 2008-01-02, 08:52
  3. Replies: 1
    Last Post: 2007-04-16, 08:57
  4. Firmware bug or code bug displaying list on 3650
    By blackjack75 in forum Mobile Java General
    Replies: 2
    Last Post: 2003-09-24, 19:52

Posting Permissions

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