Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Mouse Enter/Exit Events

1029932Aug 1 2013 — edited Aug 5 2013

Hi,

I've got a question regarding mouse enter/exit events. Specifically, I would like to know why so many mouse enter/exit events are generated. Suppose you stack multiple Panes like this:

@Override
public void start(Stage frame) throws Exception {
     Pane root = new Pane();
     frame.setScene(new Scene(root));
     addEnterExitHandlers(root, "root");

     Pane fst = new Pane();
     root.getChildren().add(fst);
     addEnterExitHandlers(fst, "first");

     Pane snd = new Pane();
     fst.getChildren().add(snd);
     addEnterExitHandlers(snd, "second");

     Rectangle rect = new Rectangle(0, 0, 100, 100);
     snd.getChildren().add(rect);
     addEnterExitHandlers(rect, "rect");

     frame.show();
}

The addEnterExitHandlers() method simply registers event handlers for MouseEvent.MOUSE_ENTERED_TARGET and MouseEvent.MOUSE_EXITED_TARGET which print the event type's name together with the specified string.

The resulting output looks like this:

root MOUSE_ENTERED

first MOUSE_ENTERED
root MOUSE_ENTERED_TARGET

second MOUSE_ENTERED
first MOUSE_ENTERED_TARGET
root MOUSE_ENTERED_TARGET

rect MOUSE_ENTERED
second MOUSE_ENTERED_TARGET
first MOUSE_ENTERED_TARGET
root MOUSE_ENTERED_TARGET

root MOUSE_EXITED

first MOUSE_EXITED
root MOUSE_EXITED_TARGET

second MOUSE_EXITED
first MOUSE_EXITED_TARGET
root MOUSE_EXITED_TARGET

rect MOUSE_EXITED
second MOUSE_EXITED_TARGET
first MOUSE_EXITED_TARGET
root MOUSE_EXITED_TARGET

This output is not accurate, because I inserted empty lines to visually group related elements.

As you can see, a MOUSE_ENTERED event is fired for every node and all parent nodes receive MOUSE_ENTER_TARGET events. I just thought that it would be sufficient to only fire the last group of enter/exit events, respectively, i.e.:

rect MOUSE_ENTERED
second MOUSE_ENTERED_TARGET
first MOUSE_ENTERED_TARGET
root MOUSE_ENTERED_TARGET

rect MOUSE_EXITED
second MOUSE_EXITED_TARGET
first MOUSE_EXITED_TARGET
root MOUSE_EXITED_TARGET

Is there a specific reason for the behavior?

Best regards,
Matthias

This post has been answered by 920892 on Aug 5 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 2 2013
Added on Aug 1 2013
3 comments
535 views