Faites un clic droit dans JavaFX?
Question
Comment puis-je détecter / gérer un clic droit dans JavaFX?
La solution
Voici une façon:
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.input.*;
var r = Rectangle {
x: 50, y: 50
width: 120, height: 120
fill: Color.RED
onMouseClicked: function(e:MouseEvent):Void {
if (e.button == MouseButton.SECONDARY) {
println("Right button clicked");
}
}
}
Stage {
title : "ClickTest"
scene: Scene {
width: 200
height: 200
content: [ r ]
}
}
Autres conseils
Si vous vous interrogez sur la manipulation faites un clic droit dans les événements JavaFX, et trouver la réponse 2009 est quelque peu dépassée maintenant ... Voici un exemple de travail en java 11 (openjfx):
public class RightClickApplication extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("Example");
Rectangle rectangle = new Rectangle(100, 100);
BorderPane pane = new BorderPane();
pane.getChildren().add(rectangle);
rectangle.setOnMouseClicked(event ->
{
if (event.getButton() == MouseButton.PRIMARY)
{
rectangle.setFill(Color.GREEN);
} else if (event.getButton() == MouseButton.SECONDARY)
{
rectangle.setFill(Color.RED);
}
});
primaryStage.setScene(new Scene(pane, 200, 200));
primaryStage.show();
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow