JavaFX Text change text on mouse click

Description

JavaFX Text change text on mouse click

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   w  w w . j  a va2s . c  o 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