2013-07-12 43 views
1

我知道我可以使用此代碼,當外殼閉合告訴,如何判斷SWT Shell是以編程方式還是由用戶關閉?

shell.addShellListener(new ShellAdapter() 
{ 
    @Override 
    public void shellClosed(ShellEvent e) 
    { 
     System.out.println("closed"); 
    } 
} 

ShellEvent對象不告訴我,殼牌是否被編程關閉或當用戶點擊X按鈕。

有沒有辦法告訴?

+1

你能向我們解釋你的用例嗎?我不明白爲什麼你必須知道這一點。你不能在你調用'shell.close()'的時候做你想做的事情嗎? – Baz

+0

我嘗試了幾種方法來弄清楚如何做到這一點。問題在於你關閉了Shell,它調用org.eclipse.swt.widgets.Decorations.closeWidget()。恐怕沒有其他辦法。 –

回答

2

我花一些時間,如果是由用戶系統產生關閉ShellEvent進行區分。

後檢查在這兩種情況下的ShelEvent用不同的值,通過了的ShellEvent的ObjectGraph唯一的變量是captureChangedDisplay類,其範圍爲default

下面的代碼應該可以幫助您找到的源ShellEvent

shell.addShellListener(new ShellAdapter() { 
     @Override 
     public void shellClosed(ShellEvent e) { 

       Field f = Display.class.getDeclaredField("captureChanged"); 
       f.setAccessible(true); 
       System.out.println("captureChanged = " + f.get(e.display)); //true = If User triggered the Event 
       System.out.println("closed"); 
     } 
    }); 
+0

我收到以下異常:'java.lang.NoSuchFieldException:captureChanged'。 – Baz

+1

@Baz我可以在'org.eclipse.swt.win32.win32.x86_3.7.0.v3735b.jar'中找到它。它可能在其他平臺的'swt.jar'中丟失。嗯! – Niranjan

+2

是的,我可以驗證它是Windows庫的一部分,但不是Linux的一部分,也不是osx庫的一部分。 – Baz

相關問題