Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mouse Listener

Status
Not open for further replies.

gibson153

Technical User
Jul 20, 2001
29
US
can an image utilize a MouseListener so that I can tell when the mouse is over the image? If so are there any simple examples available?

David
 
yep, you need to look into using the getX and getY methods of the MouseEvent class. These will give you pixel positions and you can check them against the known space filled up by your image:


testIcon1.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
triggerMenu(e);
}
public void mouseReleased(MouseEvent e)
{
triggerMenu(e);
}
public void triggerMenu(MouseEvent e)
{
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
);

This is an inner class attached to a button I have set up that uses the X and Y coordinates to display a pop-up menu at the point clicked by the mouse. But it can easily be adapted to just get the coordinates for your own use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top