JavaFX Pane add and remove controls on mouse event

Description

JavaFX Pane add and remove controls on mouse event


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
   @Override /*from w  ww  . java 2 s  . c  o  m*/
   public void start(Stage primaryStage) {
      // Create a pane
      Pane pane = new Pane();

      // Create and register the handler
      pane.setOnMousePressed(e -> {
         pane.getChildren().add(new Text(e.getX(), e.getY(),
            "(" + e.getX() + ", " + e.getY() + ")"));
      });

      pane.setOnMouseReleased(e -> {
         pane.getChildren().clear();
      });

      // Create a scene and place it in the stage
      Scene scene = new Scene(pane, 200, 200);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related