Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making composite focusable in SWT

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?

like image 205
m4tx Avatar asked Jun 04 '13 15:06

m4tx


1 Answers

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();
    }
});
like image 67
Steve K Avatar answered Oct 02 '22 15:10

Steve K