Description of Mouse enter/exit handling is not specific enough
Tom Schindl
tom.schindl at bestsolution.at
Mon Aug 12 10:07:45 PDT 2013
Hi,
I've been looking at how the mouse enter/exit is handled in JavaFX -
which is different in how it works in Swing/SWT and so I looked at the
documentation on MouseEvent which says the following:
---------8<---------
When mouse enters a node, the node gets MOUSE_ENTERED event, when it
leaves, it gets MOUSE_EXITED event. These events are delivered only to
the entered/exited node and seemingly don't go through the
capturing/bubbling phases. This is the most common use-case.
---------8<---------
In JavaFX leave means of if the mouse leaves the nodes bounds completely
you get an EXIT. Or if you flip the sides: If you move the mouse above a
node which is a child of the parent you won't see an EXIT on the
parent-node, nor you see an ENTER if you move from the sub-node once
more to the parent.
So IMHO while the current strategy of mouse enter/exit makes sense from
a SceneGraph point of view e.g. when you have graphic primitives but
reacts a bit out of order for Swing/SWT convertibles. So the
documentation needs to be a bit more specific.
To understand the difference you can run these 2 snippets:
Swing:
> JFrame f = new JFrame();
>
> JPanel p = new JPanel();
> p.setLayout(new BorderLayout());
> MouseAdapter a = new MouseAdapter() {
>
> @Override
> public void mouseExited(MouseEvent e) {
> System.err.println("EXITED: " + e.getSource());
> }
>
> @Override
> public void mouseEntered(MouseEvent e) {
> System.err.println("ENTERED: " + e.getSource());
> }
> };
> p.addMouseListener(a);
>
> JButton jl = new JButton("Hello World!!!");
> jl.addMouseListener(a);
> p.add(jl,BorderLayout.WEST);
> f.getContentPane().add(p);
> f.pack();
>
> f.setVisible(true);
FX:
> BorderPane p = new BorderPane();
> EventHandler<MouseEvent> h = new EventHandler<MouseEvent>() {
>
> @Override
> public void handle(MouseEvent event) {
> if( event.getEventType() == MouseEvent.MOUSE_ENTERED ) {
> System.err.println("ENTERED: " + event.getTarget());
> } else {
> System.err.println("EXIT: " + event.getTarget());
> }
> }
> };
> p.addEventHandler(MouseEvent.MOUSE_ENTERED, h);
> p.addEventHandler(MouseEvent.MOUSE_EXITED, h);
> Button b = new Button("Hello World");
> b.addEventHandler(MouseEvent.MOUSE_ENTERED, h);
> b.addEventHandler(MouseEvent.MOUSE_EXITED, h);
> p.setCenter(b);
>
> Scene s = new Scene(p,400,400);
> primaryStage.setScene(s);
> primaryStage.show();
Tom
More information about the openjfx-dev
mailing list