如何收到复合的孩子收到/失去焦点的通知? [英] How to be notified that a composite's child received/lost focus?

查看:126
本文介绍了如何收到复合的孩子收到/失去焦点的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SWT 复合,我需要传递给一些其他代码,将随意添加子项。有没有办法得到通知,复合的孩子收到并失去了焦点?

I have an SWT Composite that I need to pass to some other code which will add children to it at will. Is there any way to be notified that a child of the composite received and lost focus?

只是为了确保清楚,我不能添加听众到每个孩子,因为我不负责创建这些控件。可以随时添加一个小孩。

Just to make sure it's clear, I cannot add listeners to each child, because I'm not in charge of creating those controls. A child could be added at any time.

推荐答案

如Favonius所述,您可以勾选布局事件,如 SWT.Resize 以确定何时被绘制并重新计算您的子层次结构,适当添加侦听器。另一个选择只是倾听所有焦点事件,只关注那些您感兴趣的控件。

As noted by Favonius, you can hook layout events like SWT.Resize to determine when you're being painted and recompute your child hierarchy, adding listeners as appropriate. Another option is simply to listen for all focus events and only pay attention to those that are for controls that you're interested in.

显示过滤器,像监听器一样被通知事件,但是过滤器的不同之处在于它们在侦听器之前运行,他们有机会取消事件,并且在整个显示之间通知所有类型的事件。

Displays have filters which, like listeners, are notified of events, however filters differ in that they are run before listeners, they have the opportunity to cancel events, and they are notified for all of a type of event on the entire Display.

您可以使用一个过滤器来检查所有焦点事件,并确定它是否是您感兴趣的。例如:

You could thus use a Filter to examine all focus events and determine if it's one that you're interested in. For example:

public class MyControl extends Composite
{
    private final Listener focusListener;

    public MyControl(final Composite parent, final int style)
    {
        /* initialize the control... */

        focusListener = new Listener()
        {
            public void handleEvent(Event event)
            {
                if (!(event.widget instanceof Control))
                {
                    return;
                }

                boolean isOurChild = false;
                for (Control c = (Control) event.widget; c != null; c = c.getParent())
                {
                    if (c == container)
                    {
                        isOurChild = true;
                        break;
                    }
                }

                if (isOurChild)
                {
                    System.out.println("Our child is " + (event.type == SWT.FocusIn ? "focused" : "unfocused"));
                }
            }
        };

        getDisplay().addFilter(SWT.FocusIn, focusListener);
        getDisplay().addFilter(SWT.FocusOut, focusListener);

        addDisposeListener(new DisposeListener()
        {
            public void widgetDisposed(DisposeEvent e)
            {
                getDisplay().removeFilter(SWT.FocusIn, focusListener);
                getDisplay().removeFilter(SWT.FocusOut, focusListener);
            }
        });
    }
}

请记住 javadoc for 显示 关于使用过滤器的警告:

Do note the javadoc for Display's warnings about using filters:


通常应避免使用过滤器,调试和代码维护的原因。

They should generally be avoided for performance, debugging and code maintenance reasons.

显然,您正在寻找任何解决方案中的性能权衡 - 根据您的应用程序类型重新发布和您的用户的工作流程,调整大小时添加焦点侦听器可能更有意义,或者简单地聆听所有焦点事件并忽略您不感兴趣的焦点事件可能更有意义。

Obviously you're looking at performance trade-offs in either solution - depending on what type of application you're delivering and your users' workflow, it may make more sense to add focus listeners when you resize, or it may make more sense to simply listen to all focus events and ignore the ones you're not interested in.

这篇关于如何收到复合的孩子收到/失去焦点的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆