2013-06-26 41 views
-1

由於某種奇怪的原因,我需要從servlet中打開swt對話框。這甚至有可能嗎? 目前我有這個但得到「無效的線程訪問錯誤」。到目前爲止,我正在這樣做。從HttpServlet啓動SWT小部件

 Thread th=new Thread(new Runnable(){ 

     @Override 
     public void run() { 
      Shell shell = new Shell(); 
      FileDialog dialog=new FileDialog(shell); 
      dialog.setText("Enter a filename"); 
      dialog.setFilterPath("c:\\"); 
      String selected=dialog.open(); 
      log.error(selected); 
      shell.dispose(); 

     } 

    }); 
    th.start(); 

任何想法?

+0

我有特殊用途的情況下,我只是想知道,如果這在技術上是可行的代碼。 – specialscope

+0

你能解釋你的用例嗎? – Baz

+0

對不起,我不能在這裏討論我的用例。我只想知道從本地環境中的servlet創建swt widget在技術上是否可行。 – specialscope

回答

2

我不想知道奇怪的原因,在同一時間,我不能向您發出警告,你可能會碰到而在Servlet打開SWT窗口的問題,但問題是,你正在訪問SWT部件原生Thread這將無法正常工作。

您只能在UI Thread上訪問SWT

另外,您並未阻止UI Thread

PFB解決了您的問題

Thread th = new Thread(new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      Display.getDefault().syncExec(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        Display display = Display.getDefault(); 
        Shell shell = new Shell(display); 
        FileDialog dialog=new FileDialog(shell); 
        dialog.setText("Enter a filename"); 
        dialog.setFilterPath("c:\\"); 
        String selected=dialog.open(); 
        log.error(selected); 
        while (!shell.isDisposed()) 
        { 
         if (!display.readAndDispatch()) 
          display.sleep(); 
        } 

       } 

      }); 
     } 
    }); 

    th.start(); 
+0

謝謝我認爲這可能有效。我忘了創建新的顯示。 – specialscope

+0

此工作感謝! – specialscope