19

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?

m4tx
  • 4,139
  • 5
  • 37
  • 61
  • 1
    It's not a duplicate... In linked topic above, the user wants to do something totally different... He wants to skip certain controls in Composite; I want to make composite keyboard-focusable. There's no need to close my question because I don't have any answer yet. – m4tx Jun 10 '13 at 20:04
  • The point of that question is exactly the same - to tab-select the composite. I would recommend you trying the solution from that question reply. – Eugene Jun 10 '13 at 20:15
  • 3
    No... I don't want to tab-select composite. I want to make composite keyboard-focusable; make the composite catching keyboard events by clicking on it (just like you do when you click, for example, text box); I repeat - I don't want to tab-select it. And I can't try the solution from that question reply, because I don't have any controls in my composite (or, to be exact, I have 1 AWT control in this SWT composite, but I don't want to make AWT control focusable because it causes a lot of glitches...). – m4tx Jun 11 '13 at 05:10
  • 2
    "_I had to create new question because the previous one wasn't reopened_" Wrong. You don't **have** to do this. The old question wasn't reopened for a reason. Voted for close again. – Baz Jun 13 '13 at 15:48
  • @Baz what's the reason? – m4tx Jun 13 '13 at 15:49
  • Ok, good luck with this attitude then... – Baz Jun 13 '13 at 15:52
  • If your question is closed, then at least 5 people or a moderator think it's best to close it. Your new question contains almost the same content (if not less) than your previous one. If this question will not be closed that's ok for me. As I said, I wish you all the best with your question. – Baz Jun 13 '13 at 15:56
  • Can you post some code? Which component do you try to wrap? – Aaron Digulla Oct 04 '13 at 10:09

1 Answers1

3

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();
    }
});
Steve K
  • 643
  • 6
  • 8