SWT对话框自动关闭

来源:互联网 发布:mysql 查询重复记录 编辑:程序博客网 时间:2024/06/06 03:18
 SWI程序在运行的过程中,通常会出现处理某一事件的时候,需要等待一段时间,处理结束以后,主界面再响应其他的事件。在这种情况下,一般会通过显示一个提示对话框,说明该操作正在运行,请稍候,处理结束后,关闭对话框,主界面可用。
如果仅仅是弹出一个对话框,此时主程序会等待对话框的结束,然后执行对话框后面的代码,需要对话框自己适当时自己关闭,可以考虑采用多线程的方式。
我们在对话框代码中,
(1)继承了Dialog类并实现了Runnable接口;
(2)打开对话框,然后启动时间处理线程;
(3)当处理线程执行run()完毕后,关闭对话框。

在我的工程中,点击按钮以后,发送消息给客户端,客户端进行处理,间隔一段时间,
向服务端返回处理结果,在这个过程中,服务端主界面进行等待,弹出对话框(里面线程对于socket端口进行read处理),当read到内容以后,线程关闭对话框,主线程继续进行。

  1. package gui;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import org.eclipse.swt.SWT;
  5. import org.eclipse.swt.events.ShellAdapter;
  6. import org.eclipse.swt.events.ShellEvent;
  7. import org.eclipse.swt.widgets.Dialog;
  8. import org.eclipse.swt.widgets.Display;
  9. import org.eclipse.swt.widgets.Label;
  10. import org.eclipse.swt.widgets.Shell;
  11. /**
  12.  * ShowProcessDialog implements the Runnable to read the check echo result in
  13.  * other thread.
  14.  */
  15. public class ShowProcessDialog extends Dialog implements Runnable {
  16.     /**
  17.      * reads the socket to get the echo result
  18.      */
  19.     private Thread tRead;
  20.     protected Object result;
  21.     protected Shell shell;
  22.     protected Display display = getParent().getDisplay();
  23.     private DataInputStream dataIn;
  24.     private String echoStr;
  25.     
  26.     /**
  27.      * Create the dialog
  28.      * 
  29.      * @param parent
  30.      * @param style
  31.      */
  32.     public ShowProcessDialog(Shell parent, int style) {
  33.         super(parent, style);
  34.     }
  35.     /**
  36.      * Create the dialog
  37.      * 
  38.      * @param parent
  39.      */
  40.     public ShowProcessDialog(Shell parent, DataInputStream in) {
  41.         this(parent, SWT.NONE);
  42.         dataIn = in;
  43.     }
  44.     /**
  45.      * Open the dialog
  46.      * 
  47.      * @return the result
  48.      */
  49.     public Object open() {
  50.         createContents();
  51.         //start the thread to read the Agent echo result
  52.         tRead = new Thread(this);
  53.         tRead.start();
  54.         shell.open();
  55.         shell.layout();
  56.         while (!shell.isDisposed()) {
  57.             if (!display.readAndDispatch())
  58.                 display.sleep();
  59.         }
  60.         return result;
  61.     }
  62.     /**
  63.      * Create contents of the dialog
  64.      */
  65.     protected void createContents() {
  66.         shell = new Shell(getParent(), SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
  67.         shell.setSize(345181);
  68.         shell.setText("运行提示");
  69.         final Label label = new Label(shell, SWT.NONE);
  70.         label.setAlignment(SWT.CENTER);
  71.         label.setText("正在执行监测,请稍候...");
  72.         label.setBounds(557020444);
  73.     }
  74.     /**
  75.      * closes the dialog, because read Thread do this, it's
  76.      * other thread, so should close dialog through dialog
  77.      * thread using syncExec.
  78.      */
  79.     public void close() {
  80.         display.getDefault().syncExec(new Runnable() {
  81.             public void run() {
  82.                 shell.close();
  83.                 shell.dispose();
  84.             }
  85.         });
  86.     }
  87.     public void run() {
  88.         try {
  89.             System.out.println("wait for reading...");
  90.             echoStr = dataIn.readUTF();
  91.         } catch (IOException e) {
  92.             System.out.println("read from Agent exception.");
  93.         }
  94.         close();
  95.     }
  96.     
  97.     public String getEcho() {
  98.         return echoStr;
  99.     }
  100. }
原创粉丝点击