java - How to track mouse over SWT composite made from sub controls? -
i have created own control:
i want track mouse , added mousetracklistener
. unfortunately mouseenter
, mouseleave
events generated, when mouse moves on parts of composite (that label , button).
[mouse enter] - mouse enters empty space [mouse hover] - mouse on empty space [mouse exit] - mouse moved on label [mouse enter] - mouse leaves label , enters empty space [mouse hover] - mouse on empty space [mouse exit] - mouse leaves composite
how can track composite 1 complete thing instead of sub parts?
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(); } } }
if want mouse move events can add untyped event composite. should
final composite comp = new composite(shell, org.eclipse.swt.swt.none); comp.addlistener(swt.mousemove, new listener() { public void handleevent(event event) { system.out.println("hit"); } });
if add same listener instance children of mycomposite can capture mouse move events.
alternately can use display.addfilter catch mouse events , filter if ones happen on composite instance or of children.
a third option use composite.setcapture mouse events when mouse enters composite area , stop capturing when leaves.
out of these think first option best performant.
Comments
Post a Comment