Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Thread with Vaadin?

I'm trying to use Thread in my project to send emails. When I click on a Button, a Thread is started and a ProgressBar is displayed. As soon as all mails are sent, the ProgressBar doesn't disappear.

This is my code:

Button btnSendMail = new Button("Mail");
btnSendMail.addClickListener(this);
@Override
public void buttonClick(ClickEvent event) {     
    if(event.getButton() == btnSendMail){   
            sendMail();
    }
}
}    

private void sendMail(){
     List<String> list = new ArrayList<String>();
     list.add("[email protected]");
     list.add("[email protected]");
     list.add("[email protected]");

     new Thread(){
         public void run(){
             while(!isInterrupt()){
                 progressbar.setVisible(true);
                 for(String send : list){
                     new SendMailClass(send); //javamail class
                 }           
                 progressbar.setVisible(false);
                 interrupt();
    }   
}.start();


}

How can I control visibility of the ProgressBar from a separated Thread?

like image 273
FernandoPaiva Avatar asked Feb 06 '14 12:02

FernandoPaiva


People also ask

Is Vaadin framework good?

Vaadin is a mature web framework for developing rich internet applications. Building web-based GUIs with Vaadin feels like developing a desktop application, which is great, comfortable and fast. However, there are situations where Vaadin is not suitable.

Is Vaadin easy to learn?

Since Vaadin is Java based, it is fully Object oriented. A Java developer can easily develop a website, simply by having idea of Vaadin classes and its uses. Vaadin provides plug-in supports and it is very easy to learn and integrate with other web frameworks.

Is Vaadin scalable?

Collaborative workplace web apps must typically accomodate hundreds, even thousands of concurrent users without putting a strain on the servers. Building your web app on Vaadin ensures your project a great starting point for scalability.


1 Answers

To update UI elements from a background thread, you have to activate either push or polling.

The documentation can be found in the vaadin book.

https://vaadin.com/de/book/vaadin7/-/page/advanced.push.html

In addition to enabling push, you also need to synchronize access to the UI elements as described in section "11.16.3. Accessing UI from Another Thread"

like image 109
André Schild Avatar answered Oct 08 '22 18:10

André Schild