swing 长时间任务的执行提示在界面的同步提示

摘抄精要的函数:
程序是片段摘抄,括号关系不一定对,关键是思路,重点在button事件中启动线程。
btn_sync2local.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         //-----------------------------
         new Thread() {
    public void run() {
                 downloadDirtyFiles();
    }
         }.start();
         //-----------------------------
    }
});




button.addActionListener(new   ActionListener()   {
public   void   actionPerformed(ActionEvent   e)   {
   new   Thread()   {
    public   void   run()   {
       try   {
          showMessage( "step   one... ");
          Thread.sleep(3000);
          showMessage( "\nstep   two... ");
          Thread.sleep(3000);
          showMessage( "\nfinished. ");
          Thread.sleep(3000);
         }
          catch   (InterruptedException   ie)   {
             //ignored
      }
    }
    }.start();
}
});


private   void   showMessage   (final   String   msg)   {
        SwingUtilities.invokeLater(new   Runnable()   {
            public   void   run()   {
                pane.setText(pane.getText()   +   msg);
            }
        });
    }

你可能感兴趣的:(java,thread,swing)