Search code examples
java-melwuitmidp

LWUIT 1.4 : Why is there sometimes duplicates in this TextArea?


I created a class from Dialog which contains a TextArea :

public class Alert extends Dialog {
    private Container c = new Container(new BorderLayout());
    private Label titre = new Label("Mobile Banking");
    private TextArea chp;
    private Command[] comms;
    public Alert(String text, Command[] comms)
    {
        super();
        titre.setUIID("titre_alert");
        titre.setAlignment(Label.CENTER);
        this.comms = comms;
        setAutoDispose(true);
        for (int cmd=0; cmd<comms.length; cmd++)
            addCommand(comms[cmd]);
        chp = new TextArea();
        chp.setEditable(false);
        chp.setAlignment(Label.CENTER);
        chp.getSelectedStyle().setBorder(null);
        chp.getUnselectedStyle().setBorder(null);
        chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
        if (text.length() % 2 != 0)
            text = " ".concat(text);
        while (text.substring(0, (text.length()/2)+1).length() < chp.getMaxSize()/2)
        {
            text = " ".concat(text);
        }
        chp.setText(text);
        c.addComponent(BorderLayout.NORTH, titre);
        c.addComponent(BorderLayout.CENTER, chp);
    }
    public Command affiche()
    {
        return show(null, c, comms);
    }
}

Inside a Form I start a thread which makes a HttpConnection call and other tasks. If the tasks end successfully then I call the affiche() method of the above class Alert :

alert = new Alert("Chargement effectué avec succès !", new Command[]{ok});
cntnr.removeComponent(cPatienter); // container displaying the "please wait..."
repaint(); // repainting the Form
if (alert.affiche() == ok) // showing the confirmation of successfullness of the task
{
     alert.dispose();
     controller.displayScreen("Menuprincipale");
}

The problem is that , sometimes , the text shown when calling the affiche() method is duplicated : it should show only the text Chargement effectué avec succès ! but sometimes it shows the text and also Chargement effectué.

So how to make it that only the text parameter is only shown but not duplicated ?


Solution

  • You are invoking LWUIT on a separate thread which is illegal. You need to use Display.callSerially to avoid a race condition in the text layout code. Something like this:

    Display.getInstance().callSerially(new Runnable() {
       public void run() {
           // your LWUIT code here, no need for repaints 
       }
    });
    

    A better approach is to use LWUIT4IO for your networking since it does this seamlessly for you.