MouseInfo and PointerInfo is a part of Java AWT. MouseInfo provides information about the location of the pointer and the number of buttons on the mouse. PointerInfo provides information returns the information about the location of the pointer and the graphics device.
Methods of MouseInfo
|getNumberOfButtons()||Returns the number of buttons on the mouse.|
|getPointerInfo()||Returns a PointerInfo object that represents the current location and graphics device of the pointer.|
Methods of PointerInfo
|getDevice()||Returns the graphics device on which mouse was present when the object was created .|
|getLocation()||Returns a point object that gives the location of the mouse pointer.|
- Example Program to find the number of buttons on the Mouse
// Java Program to find the number of
// buttons on the mouse
// Main Method
// number of buttons in the mouse
numberofbuttons = MouseInfo.getNumberOfButtons();
// print the number of buttons
"Number of buttons on the mouse ="
Number of buttons on the mouse = 5
- Example Program to show the Position of Mouse
// Java Program to show the
// position of mouse
JLabel l, l1;
// Main Method
// create object
mouse m =
// default constructor
// create labels
// create a panel
JPanel p =
// add labels to panel
// get the pointer info object from the mouseInfo
PointerInfo pi = MouseInfo.getPointerInfo();
// get the location of mouse
Point p = pi.getLocation();
// set the text of labels
"x position ="
"y position ="
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.