Problem
You can add an event listener to the node that detects mouse movement above it. This does not work if the mouse button was pressed before you navigated through the node.
Question
Does anyone know how to detect mouse movement at the click of a button? So far, I have found a solution using the MOUSE_DRAGGED event, and then instead of using getSource () with getPickResult () and evaluating PickResult .
Here is the code including Uluk's solution. The old and new solution can be switched using useNewVersion (Uluk version) boolean:
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.input.PickResult; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Main extends Application { boolean useNewVersion= true; int rows = 10; int columns = 20; double width = 1024; double height = 768; @Override public void start(Stage primaryStage) { try { BorderPane root = new BorderPane();
In the end, you can draw using the main mouse button and erase the paint using the additional mouse button:

source share