[英]Change the active application on OS X with Java and SWT
這是我的具體情況:
我的應用程序在后台運行,我想從我的應用程序工具欄圖標的右鍵菜單中打開一個新窗口。 為簡單起見,我使用MessageDialog
,並在對話框上使用open()
方法,該窗口應顯示並使其成為活動窗口。
由於某種原因,我遇到了以下問題:如果我打開了其他應用程序,然后嘗試打開此新對話框,它將打開,但在活動應用程序后面。 新對話框將在相同應用程序的其他窗口之前打開,但不會在其他(活動)應用程序的前面打開。
有沒有一種簡單的方法可以做到這一點,而我卻缺少了?
我找到了解決方案。 本質上,您需要找到活動的工作台外殼(應用程序外殼)並在該外殼上使用以下方法,然后將實際所需的窗口推到最前面。
您可以通過在重繪之前添加方法shell.setMaximized(true)
來輕松修改此選項,以使窗口看起來也最大化。
private static void bringupDialog(WindowState state) {
final Shell workbenchShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
//bring up the application to front
workbenchShell.setVisible( true );
workbenchShell.setMinimized( false );
workbenchShell.redraw();
//focus on dialog
workbenchShell.setActive();
workbenchShell.forceActive();
workbenchShell.setFocus();
workbenchShell.forceFocus();
workbenchShell.moveAbove( null );
workbenchShell.redraw();
Shell shell = instance.getShell(); // desired window shell
shell.setActive();
shell.forceActive();
shell.setFocus();
shell.forceFocus();
shell.moveAbove( null );
shell.redraw();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.