How to update Blackberry user interface elements from a stream?

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 am trying to create a small text player.
Instead, to get all the text labels one by one, something like


display text
wait this.duration milliseconds
display text2player
wait 1000 milliseconds
display text3player
the thread is done.


The screen waits for this .duration + 1000 milliseconds and displays all the labels at once. I tried with runnable and calling .invokeLater or .invokeAndWait, but I still get the same behavior, and even if I get dirty as shown above using synchronized, it still doesn't work.

Does anyone know how I can display each shortcut at a time?

Thanks!

+4
source share
1 answer

Try moving the synchronization between sleep operations ... maybe it does not appear because you have acquired a lock, and the user interface thread cannot be updated during sleep.

Do you see latency or immunity in the user interface when your thread is sleeping? Try it like this:

 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"); } } } } 
+7
source

Source: https://habr.com/ru/post/1305894/


All Articles