All JFrame freeze while do JavaMail

倖福魔咒の 提交于 2019-11-26 10:00:46

问题


I develop program of car management system. Then I want to send mail to owner of this company when car coming in and car out. My code can send mail successfully but I notice that while mail send, other JFrame window is freeze (I can\'t do anything on all JFrame window) until mail send is complete. Is this normally for Javamail or there is a way to make other JFrame still working?

In my program, it take about 10 seconds to complete send one mail.


回答1:


When you do heavy task you should run them in another threads rather in the same as gui. If you run in Event Dispatch Thread then the gui is gonna freeze until finish.

You can use SwingWorker here is an example i really like Swing Worker Example

Example:

class Worker extends SwingWorker<String, Object> {

    @Override
    protected String doInBackground() throws Exception {
       //here you send the mail
       return "DONE";
    }

    @Override
    protected void done() {
        super.done();
        //this is executed in the EDT
        JOptionPane.showMessageDialog(null, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
    }
}


来源:https://stackoverflow.com/questions/17627104/all-jframe-freeze-while-do-javamail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!