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.
Java Code:
package tutorials;
import javax.swing.JFrame;
public class MouseMethods {
public 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.
Java Code:
package tutorials;
import java.awt.event.*;
import javax.swing.JFrame;
public 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:
Java Code:
package tutorials;
import java.awt.event.*;
import javax.swing.JFrame;
public MouseMethods() {
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
System.
out.
println("The frame was clicked.");
}
System.
out.
println("The mouse entered the frame.");
}
System.
out.
println("The mouse exited the frame.");
}
System.
out.
println("The left mouse button was pressed.");
}
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."
Java Code:
package tutorials;
import java.awt.event.*;
import javax.swing.JFrame;
public MouseMethods() {
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.addMouseListener(this);
frame.setVisible(true);
}
System.
out.
println("The frame was clicked.");
}
System.
out.
println("The mouse entered the frame.");
}
System.
out.
println("The mouse exited the frame.");
}
System.
out.
println("The left mouse button was pressed.");
}
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:
Java Code:
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 MouseMethods() {
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
label.addMouseListener(this);
button.addMouseListener(this);
frame.add(label);
frame.add(button);
frame.setVisible(true);
frame.pack();
}
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();
}
}