SWT – MouseListener & MouseAdapter Example

MouseListener

Mouse event processing is easy to implement, we need to implement MouseListener interface and declare all the interface methods.

We can attached the MouseListener to a widget with addMouseListener() method.


	"widget control".addMouseListener(new MouseListener()
	{
		public void mouseDown(MouseEvent e)
		{
			System.out.println("Mouse Down.");
		}
		public void mouseUp(MouseEvent e)
		{
			System.out.println("Mouse Up.");
		}
		public void mouseDoubleClick(MouseEvent e)
		{
			System.out.println("Mouse Double click.");
		}
	
	});

Here is the full source code.

mport org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class SWTMouseListener {
 
public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
 
	//push button
	Button pushButton = new Button(shell, SWT.PUSH);
	pushButton.setLocation(50, 50);
	pushButton.setText("Im a Push Button");
	pushButton.pack();
 
	pushButton.addMouseListener(new MouseListener()
	{
		public void mouseDown(MouseEvent e)
		{
			System.out.println("Mouse Down.");
		}
		public void mouseUp(MouseEvent e)
		{
			System.out.println("Mouse Up.");
		}
		public void mouseDoubleClick(MouseEvent e)
		{
			System.out.println("Mouse Double click.");
		}
	
	});
	
	shell.setSize(500,500);
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

MouseAdapter

Sometime we only need a double clicks method in mouse event, but we implement MouseListener interface, we have to declare all interface methods. Here MouseAdapter come in place, with MouseAdapter , it gives all MouseListener a default implementation, we only need to declare the method we want to use.

We can attached the MouseAdapter to a widget with addMouseListener() method as well.


	"widget control".addMouseListener(new MouseAdapter()
	{
		public void mouseDoubleClick(MouseEvent e)
		{
			System.out.println("Mouse Double click.");
		}
	
	});

Here is the full source code.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class SWTMouseAdapter {
 
public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
 
	//push button
	Button pushButton = new Button(shell, SWT.PUSH);
	pushButton.setLocation(50, 50);
	pushButton.setText("Im a Push Button");
	pushButton.pack();
 
	pushButton.addMouseListener(new MouseAdapter()
	{
		public void mouseDoubleClick(MouseEvent e)
		{
			System.out.println("Mouse Double click.");
		}
	
	});
	
	shell.setSize(500,500);
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
bhavika Recent comment authors
newest oldest most voted
bhavika
Guest
bhavika

thanks Mr mkyong