views:

293

answers:

2
public class PlayText extends Thread {

   private int duration;
   private String text;
   private PlayerScreen playerscrn; 

   public PlayText(String text, int duration) {
        this.duration = duration;
        this.text = text;
        this.playerscrn = (PlayerScreen)UiApplication.getUiApplication().getActiveScreen();
   }



    public void run() {
        synchronized(UiApplication.getEventLock()) {
            try{
                RichTextField text1player = new RichTextField(this.text, Field.NON_FOCUSABLE);
                playerscrn.add(text1player);
                playerscrn.invalidate();

                Thread.sleep(this.duration);

                RichTextField text2player = new RichTextField("hahhaha", Field.NON_FOCUSABLE);
                playerscrn.add(text2player);
                playerscrn.invalidate();

                Thread.sleep(1000);
                RichTextField text3player = new RichTextField("Done", Field.NON_FOCUSABLE);
                playerscrn.add(text3player);
                playerscrn.invalidate();

            }catch(Exception e){
             System.out.println("I HAVE AN ERROR");
            }   
        }
    }
}

With the above code I'm trying to create a small text player.
Instead to get all the text labels one by one, something like

display text1player
wait this.duration milliseconds
display text2player
wait 1000 milliseconds
display text3player
thread done.

The screens waits this.duration + 1000 milliseconds and displays all the labels at once. I tried with a runnable and calling .invokeLater or .invokeAndWait but I still get the same behavior, and even if I get dirty like above using synchronized it still doesn't work.
Does anyone know how I can display each label at a time?
Thank you!

+2  A: 

Try to move the synchronization between the sleep statements... perhaps it's not showing up because you've acquired the lock and the UI thread cannot update while you're sleeping.

Are you seeing a delay or unresponsiveness in the UI while your thread is sleeping? Try it this way:

public class PlayText extends Thread {

   private int duration;
   private String text;
   private PlayerScreen playerscrn; 

    public PlayText(String text, int duration) {
        this.duration = duration;
        this.text = text;
        this.playerscrn = (PlayerScreen)UiApplication.getUiApplication().getActiveScreen();
    }

    private void displayTextLabel(string textToDisplay){
        synchronized(UiApplication.getEventLock()) {
            playerscrn.add(new RichTextField(textToDisplay, Field.NON_FOCUSABLE));
            playerscrn.invalidate();
        }
    }

    public void run() {

            try{

                displayTextLabel(this.text);
                Thread.sleep(this.duration);

                displayTextLabel("hahhaha");
                Thread.sleep(1000);

                displayTextLabel("Done");

            }catch(Exception e){
             System.out.println("I HAVE AN ERROR");
            }   
        }
    }
}
Lirik
It works, Thank you!I was reading and reading BB docs for two days now, and I found nothing, stackoverflow saved some of my neurons.
Andrei T. Ursan
I used to say "google is your friend", but ever since I came to stackoverflow I've decided to say "stackoverflow is your BEST friend!" :)
Lirik
I can't agree more :).
Andrei T. Ursan
A: 

How can i get this code to work? what else i need? I have added this:

import net.rim.device.api.ui.UiApplication;
public class PlayerS extends UiApplication {
  private PlayerScreen screen;

  public PlayerS() {
    screen = new PlayerScreen();
    pushScreen(screen);
  }

  public static void main(String[] args) {
    PlayerS uiApp = new PlayerS();
    uiApp.enterEventDispatcher();
  }
}

And also this:

import net.rim.device.api.ui.container.MainScreen;

public class PlayerScreen extends MainScreen {
  private PlayText pThread;

  public PlayerScreen () {
    this.setTitle("Title");
    this.pThread = new PlayText("New Text", 1000);
    pThread.start();
  }
}
timoto