All JFrame freeze while do JavaMail

前端 未结 1 1439
囚心锁ツ
囚心锁ツ 2020-11-29 13:15

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 noti

相关标签:
1条回答
  • 2020-11-29 13:40

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题