gwt

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 {

	@Override
	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
 @Override
 public void onMouseEnter(Widget sender) {
label.setText("Mouse cursor just entered the Label widget");
   }
 // Implement the onMouseLeave method
 @Override	
 public void onMouseLeave(Widget sender) {
label.setText("Mouse cursor just left from the Label widget");
  }
 // Implement the onMouseDown method
 @Override
   public void onMouseDown(Widget sender, int x, int y) {
  	 label.setText("You clicked me!");
   }

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

    // Add label widget to Root Panel
RootPanel.get().add(label);
   }
	}

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

Ilias Tsagklis

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

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button