当鼠标进入组件控件时,如何停止SWT组合控件触发MouseExit事件? [英] How can I stop an SWT composite control from firing a MouseExit event when the mouse enters a component control?

查看:226
本文介绍了当鼠标进入组件控件时,如何停止SWT组合控件触发MouseExit事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有子类org.eclipse.swt.widgets.Composite来创建一个新的复合控件。我想在这个控件中捕获MouseEnter和MouseExit事件,但是我遇到的问题是,当鼠标悬停在控件中的组件(比如Label)上时,MouseExit事件被触发,尽管标签是整个的一部分复合。

I have subclassed org.eclipse.swt.widgets.Composite to create a new composite control. I want to capture MouseEnter and MouseExit events in this control but the problem I have is that when the mouse is hovered over a component in the control (say, a Label) the MouseExit event is fired, even though the label is part of the whole Composite.

有没有办法阻止此事件被触发?我只想看到事件,如果鼠标离开控件的总边界。以下是一些示例代码,以显示我的意思。

Is there any way to stop this event being fired? I only want to see the event if the mouse leaves the total boundary of the control. Here is some example code to show you what I mean.

public class MyControl extends Composite{

Label label;

public MyControl(Composite parent, String label) {
    super(parent, SWT.NONE);
    label = new Label(this,0);
    label.setText(label);

    this.addListener(SWT.MouseEnter, new Listener() {
        @Override
        public void handleEvent(Event event) {
            // handle this event
        }           
    });
    this.addListener(SWT.MouseExit, new Listener() {
        @Override
        public void handleEvent(Event event) {
            // handle this event
        }           
    });
}

}

推荐答案

您可以简单地在事件处理程序中放置逻辑,以查看控件是否是新控件的子项,并忽略它。以下内容:(我没有测试代码,但我认为这应该适合你)

You can simply put an logic in your event handler to see if the control is a child of your new control and ignore it. Something like the following: (I haven't tested the code, but I think this should work for you)

    this.addListener(SWT.MouseExit, new Listener() {
        @Override
        public void handleEvent(Event event) {
            for (Control control : ParentClass.this.getChildren()) {
                if (control == event.item)
                    return;
            }
            // handler logic goes here
        }           
    });

这篇关于当鼠标进入组件控件时,如何停止SWT组合控件触发MouseExit事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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