MouseEntered and MouseExited problem?
Hi there everybody,
I have problem with those two mouse listeners when I use them on JPanel! Bdw, there are two more JPanels inside this main JPanel, just to mention. Well, everything is OK until some JButton or JCheckBox is added on child JPanel, then situation becames: when mouse enters main JPanel MouseEntered is fired (that is OK) but when I move mouse over main JPanel and mouse enters JButton or JCheckBox (which is inside that main JPanel) then main JPanel fires MouseExited event! WHY!? I don't need that to happen's, please F1.
Is there any solution?
Best regards,
Miljan
If you are adding the events to the main JPanel, that is exactly what is going to happen, if you want to change that behaviour change the lister to one of those child JPanels.MeTitus
I think you need to use MouseEvent.consume() in the mouseEntered event on your button this will stop the event getting to the main panel event listener.I must stress, I THINK. No time to test, got to get back to real work!Ted.
This is a normal behaviour, since the mouse is really exiting the panel and entering the buttonCheck out this post, it has a couple of ideas how to overcome this: http://forum.java.sun.com/thread.jspa?threadID=613769&messageID=3397703
I think you didn't understand me:
- only main JPanel has MouseEntered and MouseExited
- lets take for example that inside main JPanel has two child JPanels
- first child JPanel has no components
- second child JPanel has a JButton
- when mouse enters main JPanel MouseEntered() is fired - OK
- when mouse enters JButton on child JPanel, MouseExited() is fired from main JPanel - NOT OK (for me)!
Is there any solution to main JPanel don't fire MouseExited() when mouse is moving over components on it? It does not happen's when JLabel is placed on child JPanel
I understand exactly what you're talking about.
Did you look at the link I gave you?
JButton is also handling mouse events even if you don't add a mouse listener to it.
JLabel doesn't have a mouse listener, and does not handle mouse events.
Only one component can handle the mouse event at a certain location on the screen, so when you move the mouse to the button it starts handling the mouse events and the panel stops handling the events.
That's why you get a mouseExited event.
There is and an easier solution:
public class SomePanel extends JPanel {
// ...
private class EventHandler extends MouseAdapter {
public void
mouseExited(MouseEvent e) {
// Check whether the mouse cursor is still over the panel
if(getMousePosition(true) != null) return;
// your code
}
// ...
}
}