Determining focus in SWT
Asked Answered
S

4

9

I know that I can see if a particular widget has control in SWT by using isFocusControl() on it. However, when my expected widget doesn't have focus, how can I determine what does (in other words, what took the focus away)?

I'm able to handle keyboard events with traverse listeners, but changing focus using clicks of the mouse appears to mystify my application. I can't seem to figure out how to find the item that took the focus from the previous item.

I'm also having issues with reliably setting focus to another widget from within a FocusLost listener if the focus is changed by a mouse event.

Any suggestions?

Speedy answered 7/1, 2011 at 17:12 Comment(0)
C
15

It is:

Display.getFocusControl();
Cheston answered 7/1, 2011 at 17:28 Comment(5)
OK, I tried that. This appears to return a Control. When I use toString() on it in order to read what control has focus, it simply says Text {}. Can I get any more detail from this function?Speedy
What kind of detail? Why don't you call any of the getter functions of the control, to get more detail.Cheston
Or use the getParent() function to see where in the hierarchy it fits.Cheston
I'm trying to determine the name of the Text component that took the focus away. Of the getters available to the Control, I didn't see a getter that was capable of doing that. getParent() simply told me that the element was in the same Shell as everything else.Speedy
Generally speaking its impossible. You could have n variables referencing the control, the control might be in an array or even not referenced at all. We had the same problem and used reflection to iterate through the fields of the parent component and look, which of them contains the control, as a "best guess", but this won't help always.Cheston
V
2

As explained, Display.getFocusControl() tells you which Control has focus. You can associate information with widgets via the setData() methods. You can do this with every control that could possibly get focus and then getData() should help you figure out what control has the focus.

Otherwise you can just keep pointers to the controls that you created and compare the pointer to your known control pointers, no?

Vocative answered 7/6, 2012 at 9:40 Comment(0)
B
2

Since this is a tricky one, let me add something concerning the second part of the question:

I'm also having issues with reliably setting focus to another widget from within a FocusLost listener if the focus is changed by a mouse event.

When changing focus with the mouse, the mouse event is processed after the focus events. This might cause the mouse event to revoke changes you are applying in the focus events.

For example, to select the content of a text field after the textfield gains focus by a mouse click, an async call allows to delay the selection until the events are dispatched.

textfield.addFocusListener(new FocusListener() {            
  @Override
  public void focusLost(FocusEvent e) {
  }         
  @Override
  public void focusGained(FocusEvent e) {
    Display.getCurrent().asyncExec(new Runnable() {
      @Override
      public void run() {
        if (!textfield.isDisposed()) {
          textfield.selectAll();
        }
      }
    }); 
  }
});

Without the async call, the mouse event revokes the selection done in the focus event.

Bust answered 12/8, 2015 at 11:35 Comment(0)
L
1

getFocusControl returns a Control and your item inherits from Control. I use a bunch of custom controls and when I get which has focus I then determine what class it really is by using a set of if( control instanceof myclass) statements (else ifs after the first) Then once I have the real class i then cast to that class and call the proper getter methods I put into my class.

Hope this helps,

Lachrymose answered 5/9, 2012 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.