I'm working on an Eclipse RCP app and I want to steal the focus from an embedded editor (which is just a fancy text fi开发者_开发问答eld, actually) within a view, when starting an action from the same view's toolbar. That action works with a command. This editor/textfield loses the focus automatically, when the view itself loses the focus. So how do I steal the focus from the view?
I'm not sure if this is quite what you are looking for, but we use code like this when closing an editor or shutting down the application to "force-finish" a user's outstanding UI field edit so that validation and model update happen before dirty state is checked (so that we don't silently "lose" users' outstanding edits).
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null)
return;
final Display display = workbench.getDisplay();
if (display == null)
return;
final Control focusControl = display.getFocusControl();
if (focusControl == null)
return;
focusControl.notifyListeners(SWT.FocusOut, null);
I don't know if this is the "best" or "proper" way, but it works, at least in 3.2.
精彩评论