I have my own control:

I want to track a mouse and add a MouseTrackListener . Unfortunately, MouseEnter and MouseLeave events MouseEnter also generated when the mouse moves over parts of my composite element (this is a label and a button).

[Mouse enter] - mouse enters the empty space [Mouse hover] - mouse is over the empty space [Mouse exit] - mouse moved over label [Mouse enter] - mouse leaves label and enters empty space [Mouse hover] - mouse over empty space [Mouse exit] - mouse leaves composite
How can I track a composite as one part, not subcomponents?
public class MyComposite extends Composite { public MyComposite(final Composite parent, final int style) { super(parent, style); final Label lbl = new Label(this, SWT.NONE); lbl.setBounds(10, 10, 78, 15); lbl.setText("My Composite"); final Button btn = new Button(this, SWT.NONE); btn.setBounds(190, 29, 75, 25); btn.setText("Ok"); pack(); } public static void main(final String[] args) { final Shell shell = new Shell(Display.getDefault()); shell.setText("Testcase"); shell.setLayout(new FillLayout()); final MyComposite comp = new MyComposite(shell, SWT.NONE); comp.addMouseTrackListener(new MouseTrackListener() { @Override public void mouseHover(final MouseEvent e) { System.out.println("[Mouse hover]"); } @Override public void mouseExit(final MouseEvent e) { System.out.println("[Mouse exit]"); } @Override public void mouseEnter(final MouseEvent e) { System.out.println("[Mouse enter]"); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }
source share