Making composite focusable in SWT

眉间皱痕 提交于 2019-12-22 02:32:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!