Wednesday 7 December 2016

SWT: Mouse listeners

There are 3 types of mouse listeners.
a.   MouseListener
b.   MouseMoveListener
c.    MouseTrackListener

MouseListener
MouseListener is used to track mouse click events. MouseListener interface provides following methods.

Method
Description
public void mouseDoubleClick(MouseEvent e)
Sent when mouse button is double clicked
public void mouseDown(MouseEvent e)
Sent when a mouse button is pressed.
public void mouseUp(MouseEvent e)
Sent when a mouse button is released.

Ex:
text.addMouseListener(new MouseListener() {

         @Override
         public void mouseDoubleClick(MouseEvent e) {
                 System.out.println("Mouse double clicked");
         }

         @Override
         public void mouseDown(MouseEvent e) {
                 System.out.println("Mouse pressed down");
         }

         @Override
         public void mouseUp(MouseEvent e) {
                 System.out.println("Mouse is released");
         }

});

MouseMoveListener
MouseMoveListener is used to keep track of mouse move events. MouseMoveListener has only one method mouseMove, used to track mouse move event.

Method
Description
public void mouseMove(MouseEvent e)
Sent when the mouse moves.

Ex:
text.addMouseMoveListener(new MouseMoveListener() {

         @Override
         public void mouseMove(MouseEvent e) {
                 System.out.println("Mouse moved");
         }

});

MouseTrackListener
MouseTrackListener is used to track mouse enter, exit and hover movements. MouseTrackListener interface provides following methods.

Method
Description
public void mouseEnter(MouseEvent e)
Sent when the mouse pointer passes into the area of the screen covered by a control.
public void mouseExit(MouseEvent e)
Sent when the mouse pointer passes out of the area of the screen covered by a control.
public void mouseHover(MouseEvent e)
Sent when the mouse pointer hovers over a control.

text.addMouseTrackListener(new MouseTrackListener() {

         @Override
         public void mouseEnter(MouseEvent e) {
                 System.out.println("Mouse Entered");

         }

         @Override
         public void mouseExit(MouseEvent e) {
                 System.out.println("Mouse Exited");

         }

         @Override
         public void mouseHover(MouseEvent e) {
                 System.out.println("Mouse Hover");

         }

});


Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Test {

 private static int shellWidth = 400;
 private static int shellHeight = 400;

 private static void addWidgetsToShell(Display display, Shell shell) {

  Text text = new Text(shell, SWT.MULTI | SWT.BORDER);
  text.setBounds(10, 10, 300, 100);
  text.setEditable(false);
  text.setText("Click me to see the affect in console");

  text.addMouseListener(new MouseListener() {

   @Override
   public void mouseDoubleClick(MouseEvent e) {
    System.out.println("Mouse double clicked");
   }

   @Override
   public void mouseDown(MouseEvent e) {
    System.out.println("Mouse pressed down");
   }

   @Override
   public void mouseUp(MouseEvent e) {
    System.out.println("Mouse is released");
   }

  });

  text.addMouseMoveListener(new MouseMoveListener() {

   @Override
   public void mouseMove(MouseEvent e) {
    System.out.println("Mouse moved");
   }

  });

  text.addMouseTrackListener(new MouseTrackListener() {

   @Override
   public void mouseEnter(MouseEvent e) {
    System.out.println("Mouse Entered");

   }

   @Override
   public void mouseExit(MouseEvent e) {
    System.out.println("Mouse Exited");

   }

   @Override
   public void mouseHover(MouseEvent e) {
    System.out.println("Mouse Hover");

   }

  });
 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment