JavaFX Pane add control on mouse event

Description

JavaFX Pane add control 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 w  w  .ja  va2  s.com*/
  public void start(Stage primaryStage) {
    // Create a pane
    Pane pane = new Pane();

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

    // 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