wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes How to do a Mouseover.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to do a Mouseover.." Watch "How to do a Mouseover.." New topic

How to do a Mouseover..

Rocky Summers
Ranch Hand

Joined: Nov 07, 2003
Posts: 66
I'm curious... is there any way i can add a "Mouseover" to a JLabel so that when the mouse is over it, the cursor changes to a hand-cursor?
Lalit K Kumar
Ranch Hand

Joined: Jan 29, 2004
Posts: 32
I think you can add a "mouseover" on a jlabel. To do so you will have to implement "MouseListener" class.Then to the respective label add the listener (.addMouseListener) and implement the required methods ("mouseOver" in your case).
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Ummm... actually the interface you want to look at is MouseMotionListener... and the method you'll want to use is mouseMoved(). You could also try setting a boolean in mouseEntered() and unsetting it in mouseExited() of the MouseListener interface.

You could also use a JButton instead of a JLabel and use the built in methods - setRolloverEnabled(), setRolloverIcon(), etc.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
You can do this. Add a mouseListener to a JLabel and change the cursor on mouseEntered and mouseExited.
I have a custom class that does just this - if anyone wants to see the code, let me know.

My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Rocky Summers
Ranch Hand

Joined: Nov 07, 2003
Posts: 66
Brian I do!
It is sorta covered in the JavaRanch Style Guide.
subject: How to do a Mouseover..