0

I got my MouseMotionListener to work. My problem now is that I would like to change the color of my text (initially black) to another color (say red) after the text is hovered over.

Then it has to go back to the original color after my mouse is not hovering over that text anymore.

Any tips? Thank you in advance!

Nick Rippe
  • 6,465
  • 14
  • 30

1 Answers1

3

A MouseMotionListener is not what you want here. Use a standard MouseListener and use the mouseEntered and mouseExited methods to set the label color. Ideally the dimensions of the text should match those of the label itself so be sure to use a layout manager that respects preferred sizes and pack your top level container on startup.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • @mKorbel what, using a `MouseListener` ? – Reimeus Mar 06 '14 at 19:57
  • (Changing the Color of a Text on Hover) in JLabel isn't repaint() in API for MouseEvent(s), it must be called programatically – mKorbel Mar 06 '14 at 20:00
  • Looking at source for `Component` it does call repaint for `setForeground` :)) – Reimeus Mar 06 '14 at 20:04
  • yes right for JComponent, but isn't valid just for JLabel (a few times here by kleopatra & me) – mKorbel Mar 06 '14 at 20:06
  • Interesting, read your [answer](http://stackoverflow.com/questions/20835173/implementing-a-simple-hover-effect-on-a-jlabel#20835363), did a quick check and worked though, so `repaint` required for consistently reliable (cross platform, etc.) refresh? – Reimeus Mar 06 '14 at 20:12