Home » Enterprise Java » gwt » MouseListener Example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

MouseListener Example

In this example we shall show you how to create a MouseListener example using the Google Web Toolkit, that is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. The MouseListener is an Event listener interface for mouse events.To create a MouseListener we have performed the following steps:

  • The MouseListenerExample class implements the com.google.gwt.core.client.EntryPoint interface to allow the class to act as a module entry point. It overrides its onModuleLoad() method.
  • Create a Label widget.
  • Attach a new MouseListener to the Label. Implement the MouseListener methods, onMouseEnter(Widget sender), onMouseLeave(Widget sender), onMouseDown(Widget sender, int x, int y), onMouseMove(Widget sender, int x, int y) and onMouseUp(Widget sender, int x, int y) methods.
  • Add the Label to the Label to the RootPanel, that is the panel to which all other widgets must ultimately be added,

as described in the code snippet below. 

package com.javacodegeeks.snippets.enterprise;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class MouseListenerExample implements EntryPoint {

	public void onModuleLoad() {
		// Create Label widget
	    final Label label = new Label("Play with me");

  // Attach mouse listener to label
label.addMouseListener(new MouseListener() {

 // Implement the onMouseEnter method
 public void onMouseEnter(Widget sender) {
label.setText("Mouse cursor just entered the Label widget");
 // Implement the onMouseLeave method
 public void onMouseLeave(Widget sender) {
label.setText("Mouse cursor just left from the Label widget");
 // Implement the onMouseDown method
   public void onMouseDown(Widget sender, int x, int y) {
  	 label.setText("You clicked me!");

 // onMouseMove method (does nothing)
 public void onMouseMove(Widget sender, int x, int y) {
				// do nothing
 // onMouseUp method (does nothing)
			 public void onMouseUp(Widget sender, int x, int y) {
				// do nothing

    // Add label widget to Root Panel

This was an example of how to create a MouseListener example using the Google Web Toolkit.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments