I've implemented a simple mouse listener where the background color changes whenever the mouse enters the component (a JPanel), and it reverts back whenever the mouse leaves. This has some problems:
I'm guessing this is an easy one for Swing veterans. Any suggestions on how to fix this? I'd love not to use timers and such...
If I move the mouse over to the childs quickly, the mouseEnter event is not fired
I've never seen this to happen, but if it is an issue then you can handle mouseMoved instead to reset the background.
If my component has childs, when the mouse moves to the childs it triggers the mouseExit
Use the following test and the code will only be executed when you leave the components bounds:
public void mouseExited(MouseEvent e)
{
if (! getVisibleRect().contains(e.getPoint()) )
{
setBackground(...);
}
}