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(...);
}
}
There are a number of solutions:
AWTEventListener
to the default Toolkit
and filter through for the events you are interested in. This unfortunately requires a security permission.EventQueue
and filter events. This requires a security permission, put applets and WebStart/JNLP applications get that permission anyway.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With