JavaFX StackPane check if contains a control

Description

JavaFX StackPane check if contains a control

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/*w ww.java  2s . c  o m*/
   public void start(Stage primaryStage) {
      StackPane pane = new StackPane();
      Text text1 = new Text("message 1 from demo2s.com");
      Text text2 = new Text("message 2 from demo2s.com");
      pane.getChildren().addAll(text1);

      pane.setOnMouseClicked(e -> {
         if (pane.getChildren().contains(text1)) {
            pane.getChildren().add(text2);
            pane.getChildren().remove(text1);
         }
         else {
            pane.getChildren().add(text1);
            pane.getChildren().remove(text2);
         }
      });

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



PreviousNext

Related