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

[615 byte] By [Miljan_Radovica] at [2007-11-26 19:10:47]
# 1
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
Me_Titusa at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 2
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.
ted_trippina at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 3
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
Rodney_McKaya at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 4

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

Miljan_Radovica at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

Rodney_McKaya at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 6
So, there is no happy for me? :( I'm kiddin' thanks!
Miljan_Radovica at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 7
You can use the suggestion by Michael_Dunn in the link I gave you to know if the mouse really exited your JPanel, or just entered another component inside this panel.
Rodney_McKaya at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...
# 8

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

}

// ...

}

}

grigor.ilieva at 2007-7-9 21:07:12 > top of Java-index,Java Essentials,Java Programming...