Question

In the following code I demonstrate a difference between the java and javaFX2 as far as it concerns generation of a MOUSE_CLICKED event which I do not know if it should be expected or can be considered as bug.

It seems that in JavaFX 2.0 you can press the mouse button, move the mouse for as long as you like and then when you release the button a mouseClicked event will be fired. In contrast to JAVA where if after clicking the mouse button, you move the mouse and then release the button, the MouseClicked event won't be fired.

To prove this try the following code, where when clicking the mouse a rectangle is drawn at the click point. Even if you press the left button, move the mouse and then release the button the rectangle will be drawn (in the point where you released the mouse button)...

public class MouseClickTester extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    final Group root = new Group();
    Rectangle rect = new Rectangle(0, 0, 300, 300);
    rect.setFill(Color.RED);
    rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent me) {
            Rectangle yellowRect = new Rectangle(me.getSceneX(), me.getSceneY(),10,50);
            yellowRect.setFill(Color.YELLOW);
            root.getChildren().add(yellowRect);
        }
    });

    root.getChildren().add(rect);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}
}

The following code does the same, but in simple Java. But this time if you move the mouse while the left button is pressed and then you release it no rectangle will be drawn as no mouseClicked event will be fired.

public class FrameClass extends JFrame{

public FrameClass() throws HeadlessException {
    setPreferredSize(new Dimension(300,300));
    final Canvas canv = new Canvas();
    canv.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            canv.getGraphics().drawRect(e.getX(), e.getY(), 10, 50);
        }

    });

    getContentPane().add(canv);
    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {
    new FrameClass();
}
}

So the question itself: How this difference can be justified? Who is behaving right and who is wrong? And in aftermath, How can I emulate the mouseClicked in javaFX 2,0 to behave the same as in Java?

Thanks for the help!

PS. the imports are missing!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top