问题
Is it possible to create a focusable composite in SWT? I'm catching all keyboard events via Display filter, but there are some problems when the focus is on the tree or list - GTK+'s default action is to search in the contents of the control.
What I want to do is to mix SWT and AWT with focusable AWT component. I managed to make the AWT widget unfocusable and I added Display filter to make the AWT component receiving keyboard events (but not directly), even when it's not focused. But there are several problems when some SWT controls are focused - that's why I want to make composite focusable.
So my final question is: is it possible to make SWT composite focusable?
回答1:
If a Composite
contains child widgets, the default action is to give up focus when it is selected. To circumvent this, start by extending the Composite
class as such:
class FocusableComposite extends Composite
{
public FocusableComposite(Composite parent, int style)
{
super(parent, style);
}
public boolean setFocus()
{
return super.forceFocus();
}
}
Then use a MouseListener
on a new instantiation of FocusableComposite to call setFocus() directly whenever the Composite
is clicked:
Composite composite = new FocusableComposite(shell, SWT.NONE);
composite.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent event)
{
((Composite)event.widget).setFocus();
}
});
来源:https://stackoverflow.com/questions/16921614/making-composite-focusable-in-swt