Mouse events are events thrown by the mouse. Mouse events are in two types. We have the MouseListener events and the MouseMotionListener events. The MouseListener handles events created when the mouse is stationary or is not in motion.
MouseListener events are invoked when the mouse is stationary. That is, when the mouse is motionless and stable. This generates events such as mousePressed, mouseReleased, mouseClicked, mouseExited and mouseEntered.
For these events to be realized, an object of the class that holds these methods must be registered with the component you want the events to take place on using the method addMouseListener().
import statement for MouseListener interface
If you use the MouseListener interface in your program, you should have the this import statement. This makes the interface available for your program’s use.
1 |
import java.awt.event.MouseListener; |
MouseListener Declaration
1 |
public interface MouseListener extends EventListener |
The getX() and getY() methods
The getX() method returns the X coordinate of the mouse when an event occurs. The getY() returns the Y coordinate of the mouse when an event occurs.
Program to demonstrate handling of MouseListener event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
package villagecoder; import java.awt.GridLayout; import java.awt.Panel; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MouseStationary extends JFrame{ private JPanel panel; //JPanel to hold the components private JLabel clickedLabel; //label for when mouse is clicked private JLabel pressedLabel; //label for when mouse is clicked private JLabel enteredLabel; //label for when mouse is clicked private JLabel releasedLabel; //label for when mouse is clicked private JLabel exitedLabel; //label for when mouse is clicked private int WINDOW_WIDTH=400; private int WINDOW_HEIGHT=200; public MouseStationary(){ //set the title of the JFrame setTitle("MouseListener Demonstration"); //set the default close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //add mouse listener to the JFrame addMouseListener(new MyMouseListener()); //build panel buildPanel(); //add panel add(panel); //show the frame setVisible(true); } /* * This method creates the panel and adds the components */ public void buildPanel(){ //create a JPanel panel=new JPanel(); //set the layout of the frame panel.setLayout(new GridLayout(5,1)); //create a label for when mouse is clicked clickedLabel=new JLabel("no event yet", JLabel.CENTER); //create a label for when mouse is clicked pressedLabel=new JLabel("no event yet", JLabel.CENTER); //create a label for when mouse is clicked releasedLabel=new JLabel("no event yet", JLabel.CENTER); //create a label for when mouse is clicked enteredLabel=new JLabel("no event yet", JLabel.CENTER); //create a label for when mouse is clicked exitedLabel=new JLabel("no event yet", JLabel.CENTER); //add the labels to the panel panel.add(clickedLabel); panel.add(pressedLabel); panel.add(releasedLabel); panel.add(enteredLabel); panel.add(exitedLabel); } /* * Private inner class to respond to MoustListener events */ class MyMouseListener implements MouseListener{ public void mouseClicked(MouseEvent e){ //show the coordinates of the mouse where it was clicked clickedLabel.setText("Mouse was clicked at: " + "X: " + e.getX() + " and Y: " + e.getY()); } public void mousePressed(MouseEvent e){ //show the coordinates of the mouse where it was pressed pressedLabel.setText("Mouse was pressed at: " + "X: " + e.getX() + " and Y: " + e.getY()); } public void mouseReleased(MouseEvent e){ //show the coordinates of the mouse where it was released releasedLabel.setText("Mouse was released at: " + "X: " + e.getX() + " and Y: " + e.getY()); } public void mouseEntered(MouseEvent e){ //show the coordinates of the mouse where it was entered enteredLabel.setText("Mouse was entered at: " + "X: " + e.getX() + " and Y: " + e.getY()); } public void mouseExited(MouseEvent e){ //show the coordinates of the mouse where it was exited exitedLabel.setText("Mouse was exited at: " + "X: " + e.getX() + " and Y: " + e.getY()); } } /* * main method */ public static void main(String[] args) { //create instance of the class new MouseStationary(); } } |
Explanation of code
Line 3-8: import statements import a set of classes for use in the program. These include the GridLayout, JPanel, MouseEvemt etc
Line 11-16: Reference variables of some swing components are created to reference their respective objects later in the program
Line 18-19: Two final constants are created in memory. These will be used to set the width and height of the JFrame window
Line 23: The title bar of the JFrame window is set using the setTitle to “MouseListener Demonstration”
Recent Comments