Issue
I'm new to javafx.
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
mouseCoordination.setOnMouseMoved(e ->
mouseCoordination.setText(e.getX()+","+ e.getY())
);
StackPane root = new StackPane();
root.setPrefHeight(100);
root.setPrefWidth(400);
root.getChildren().add(mouseCoordination);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Here i tried to get mouse coordination, So i wrote this but it just work in short range like (0,0) to (48,17) and it doesn't update label after that. i actually wrote that in base of a question in site
Solution
That is because the label isn't width
× height
and you added the mouse listener to the label; if you add the listener to root
, it will work the way you wanted.
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
StackPane root = new StackPane();
root.setPrefWidth(400);
root.setPrefHeight(100);
root.getChildren().add(mouseCoordination);
root.setOnMouseMoved(e -> {
mouseCoordination.setText(e.getX() + "," + e.getY());
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Just to be clear, you added the mouse listener to the label which only has a width of 30 and a height of 40, for example; the size depends on the text being displayed and how much space it takes up. The mouse listener is only called if your mouse is on the label; however, since you wanted it to show your current mouse position, it wouldn't work unless the label had the same width and height as the window.
Answered By - Suic
Answer Checked By - Marilyn (JavaFixing Volunteer)