How can I stop an SWT composite control from firing a MouseExit event when the mouse enters a component control?

点点圈 提交于 2019-12-11 06:15:36

问题


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
        }           
    });
}

}


回答1:


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
        }           
    });


来源:https://stackoverflow.com/questions/2400222/how-can-i-stop-an-swt-composite-control-from-firing-a-mouseexit-event-when-the-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!