Mouse Move Listener Example : Mouse Key « SWT JFace Eclipse « Java






Mouse Move Listener Example

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MouseMoveListenerExample {
  final Display d;

  final Shell s;

  public MouseMoveListenerExample() {
    d = new Display();
    s = new Shell(d);

    s.setSize(250, 200);
    
    s.setText("A MouseListener Example");
    final Button b = new Button(s, SWT.PUSH);
    b.setText("Push Me");
    b.setBounds(20, 50, 55, 25);
    s.open();

    b.addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent e) {
        Random r = new Random(System.currentTimeMillis());
        Point p = s.getSize();
        int newX = r.nextInt(p.y);
        int newY = r.nextInt(p.x);
        b.setBounds(newX - 55, newY - 25, 55, 25);
      }

    });

    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }

  public static void main() {
    new MouseMoveListenerExample();
  }

}


           
       








Related examples in the same category

1.Mouse Track Example
2.Mouse Listener Example
3.SWT Mouse: A tracker (drag when 'torn off')SWT Mouse: A tracker (drag when 'torn off')
4.SWT Mouse : drag on mouse downSWT Mouse : drag on mouse down
5.UI Automation (for testing tools) snippet: post key eventsUI Automation (for testing tools) snippet: post key events
6.UI Automation (for testing tools) snippet: post mouse eventsUI Automation (for testing tools) snippet: post mouse events
7.Detect mouse enter, exit and hover eventsDetect mouse enter, exit and hover events
8.Intercept mouse events (drag a button with the mouse)Intercept mouse events (drag a button with the mouse)
9.How to implement hover help feedback using the MouseTrackListenerHow to implement hover help feedback using the MouseTrackListener