JavaFX Text handle mouse click event

Description

JavaFX Text handle mouse click event

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

public class Main  extends Application {

    @Override//from ww  w  .ja va  2  s. co  m
    public void start(Stage primaryStage) throws Exception {

        String s1 = "message 1";
        String s2 = "message 2";

        Text text = new Text("start");
        text.setOnMouseClicked(e -> {
            if (text.getText().contains(s1)) {
                text.setText(s2);
            } else {
                text.setText(s1);
            }
        });
        StackPane pane = new StackPane(text);

        primaryStage.setScene(new Scene(pane, 300, 300));
        primaryStage.setTitle("Click to change text");
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}



PreviousNext

Related