Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Java: MouseListener

timer mouselistener

  • Please log in to reply
3 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 23 September 2007 - 07:01 PM

Any program that actually does something is event driven. With out events, your program is entirely static and probably does nothing. While some events might be caused by an internal timer, most events are caused by the user. This tutorial will show you how to use the methods given to use my the MouseListener class.

First we are going to create a simple swing GUI. Since this tutorial already assumes you know how to do that, I won't go into details on this subject.

package tutorials;

import javax.swing.JFrame;

public class MouseMethods {

	public MouseMethods() {
		JFrame frame = new JFrame("MouseMethods");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);

		
		frame.setVisible(true);
		
	}

	public static void main(String args[]) {
		new MouseMethods();
	}
	
}

Next in the class header we will implement and import the java.awt.event package.


package tutorials;

import java.awt.event.*;
import javax.swing.JFrame;

public class MouseMethods implements MouseListener {

	public MouseMethods() {
		JFrame frame = new JFrame("MouseMethods");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);

		
		frame.setVisible(true);
		
	}

	public static void main(String args[]) {
		new MouseMethods();
	}
	
}

Since we are implementing the class, we need to use all the methods provided to us by MouseListener which, in no specific order are: mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), and mouseReleased(). The difference between mouseClicked() and mousePressed()/mouseReleased() are minimal, its just that mouseClicked() is a mouse press and mouse release, which mousePressed() and mouseReleased just listen for the mouse being pressed and released respectively. So after implementing the methods or class looks something like this:

package tutorials;

import java.awt.event.*;
import javax.swing.JFrame;

public class MouseMethods implements MouseListener {

	public MouseMethods() {
		JFrame frame = new JFrame("MouseMethods");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);

		
		frame.setVisible(true);
		
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("The frame was clicked.");
		
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("The mouse entered the frame.");
		
	}

	public void mouseExited(MouseEvent e) {
		System.out.println("The mouse exited the frame.");
		
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("The left mouse button was pressed.");
		
	}

	public void mouseReleased(MouseEvent e) {
		System.out.println("The left mouse button was released.");
		
	}
	
	public static void main(String args[]) {
		new MouseMethods();
	}
	
}

The last step we must do is add a mouseListener to an object. Most J-Objects [JLabels, JFrames, JButton, ect...] I am going to add a mouse listener to or main frame "frame."

package tutorials;


import java.awt.event.*;
import javax.swing.JFrame;

public class MouseMethods implements MouseListener {

	public MouseMethods() {
		JFrame frame = new JFrame("MouseMethods");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);
		frame.addMouseListener(this);
		
		frame.setVisible(true);
		
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("The frame was clicked.");
		
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("The mouse entered the frame.");
		
	}

	public void mouseExited(MouseEvent e) {
		System.out.println("The mouse exited the frame.");
		
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("The left mouse button was pressed.");
		
	}

	public void mouseReleased(MouseEvent e) {
		System.out.println("The left mouse button was released.");
		
	}
	
	public static void main(String args[]) {
		new MouseMethods();
	}
	
}

And thats all you need to do. Now since each MouseListener method (mouseClicked, MouseEntered, ect..) is passed a MouseEvent "e" you can expand the capabilities of the methods. For example if you wanted to check which object is being clicked, it might look something like this:

package tutorials;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseMethods implements MouseListener {


	private JLabel label = new JLabel("This is a JLabel");
	private JButton button = new JButton("This is a JButton");
	
	public MouseMethods() {
		JFrame frame = new JFrame("MouseMethods");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new FlowLayout());
		
		label.addMouseListener(this);
		button.addMouseListener(this);
		
		frame.add(label);
		frame.add(button);
		frame.setVisible(true);
		frame.pack();
		
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getSource().equals(button)){
			System.out.println("The JButton was clicked...");
		} else if(e.getSource().equals(label)){
			System.out.println("The JLabel was clicked...");
		} else {
			System.out.println("Something else was clicked...");
		}
		
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}
	
	public static void main(String args[]) {
		new MouseMethods();
	}
	
}

Originally posted as MoustListener

Edited by John, 01 August 2010 - 08:33 AM.

  • 0

#2 ElPolloDiablo

ElPolloDiablo

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 24 September 2007 - 06:07 AM

It might also be interesting to explain its derived adapter class.

The MouseListener interface has an abstract class implementation called MouseAdapter.

The advantage of using the adapter class instead of the interface is that when you only need one method (say, mouseClicked, as in the final example), you don't need to implement the other ones.

The code, when using MouseAdapter, would then look like this:
      
package tutorials;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseMethods extends MouseAdapter {

private JLabel label = new JLabel("This is a JLabel");
private JButton button = new JButton("This is a JButton");

public MouseMethods() {
JFrame frame = new JFrame("MouseMethods");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
label.addMouseListener(this);
button.addMouseListener(this);

frame.add(label);
frame.add(button);
frame.setVisible(true);
frame.pack();
}

public void mouseClicked(MouseEvent e) {
if(e.getSource().equals(button)){
System.out.println("The JButton was clicked...");
} else if(e.getSource().equals(label)){
System.out.println("The JLabel was clicked...");
} else {
System.out.println("Something else was clicked...");
}
}

public static void main(String args[]) {
new MouseMethods();
}
}

  • 0

#3 krishnangce23

krishnangce23

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 22 February 2008 - 01:05 AM

simply superb
  • 0

#4 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 22 February 2008 - 08:48 AM

Thanks, glad I could help :)
  • 0





Also tagged with one or more of these keywords: timer, mouselistener