6 Replies Latest reply: Jun 10, 2011 5:05 AM by Zlaja RSS

    Notification about Node visibility

    Zlaja
      Hi

      I use Group node to create composition of nodes. Is there any way to get notification when the composition node is visible?
      I want to start an animation when the composition node became visible.

      I have tried by adding change listener to the visibleProperty of the group node but it never calls.

      Thanks for any idea.

      Zlaja
        • 1. Re: Notification about Node visibility
          Narayan
          Hello Zlaja ,
          If the nodes changes it's visibility state then Visibily ChangeListener must be added of Node not of a group.
          You can show some sample code of yours so that we can identify your problem more easily.

          Thanks.
          Narayan
          • 2. Re: Notification about Node visibility
            Zlaja
            Here is code :
            public class MessageBox extends Group{
                
                private FadeTransition fadeTransition = null;
                 
                
                public MessageBox(String message, double size){
                    Circle circle = new Circle(size);
                    circle.setFill(gradient.build());
                    
                    
                    Text text = new Text(message);
                    text.setStyle("-fx-text-fill: #FFFFFF;-fx-font-size:26pt;");
            
                    StackPane pane = new StackPane();
                    pane.getChildren().add(circle);
                    pane.getChildren().add(text);
                    getChildren().add(pane);
            
                    fadeTransition = new FadeTransition();
                    fadeTransition.setNode(circle);
                    fadeTransition.setFromValue(1.0);
                    fadeTransition.setToValue(0.3);
                    fadeTransition.setCycleCount(6);
                    fadeTransition.setAutoReverse(true);
                    fadeTransition.setDuration(Duration.valueOf(2000));
                    
                    visibleProperty().addListener(new ChangeListener<Boolean>() {
            
                        @Override
                        public void changed(ObservableValue<? extends Boolean> value,
                                Boolean arg1, Boolean arg2) {
                    
                            if (value.getValue().equals(Boolean.TRUE)){
                                fadeTransition.play();    
                            }
                            
                        }
            
                    });  
            
            }
            At some other place I just put instance of class above in BorderPane.

            Edited by: Zlaja on Jun 9, 2011 6:00 AM
            • 3. Re: Notification about Node visibility
              jojorabbit
              Hi,
              here is code almost same as yours it works, i set cycleCount ot indefinite, it starts to play immediately when u create new instance, if you want to start it from outside just make getter for fadeTransition and start it somewhere lese.
                  public class MessageBox extends Group {
              
                      private FadeTransition fadeTransition = null;
              
                      public MessageBox(String message, double size) {
                          Circle circle = new Circle(size);
                          circle.setFill(Color.RED);
              
              
                          Text text = new Text(message);
                          text.setStyle(""
                                  + "-fx-text-fill: #FFFFFF;"
                                  + "-fx-font-size:26pt;");
              
                          StackPane pane = new StackPane();
                          pane.getChildren().add(circle);
                          pane.getChildren().add(text);
                          getChildren().add(pane);
              
                          fadeTransition = new FadeTransition();
                          fadeTransition.setNode(circle);
                          fadeTransition.setFromValue(1.0);
                          fadeTransition.setToValue(0.3);
                          fadeTransition.setCycleCount(Timeline.INDEFINITE);
                          fadeTransition.setAutoReverse(true);
                          fadeTransition.setDuration(Duration.valueOf(2000));
              
                          fadeTransition.playFromStart();
              
                      }
                  }
              Hope it helps.
              • 4. Re: Notification about Node visibility
                shakir.gusaroff
                The following works for me:

                MessageBox.java
                import javafx.application.Application;
                
                import javafx.scene.Group;
                
                
                import javafx.scene.layout.*;
                import javafx.animation.*;
                import javafx.scene.shape.*; 
                import javafx.scene.text.*;
                import javafx.util.Duration;
                import javafx.beans.property.*;
                import javafx.beans.value.*;
                        
                public class MessageBox extends Group{
                    
                    private FadeTransition fadeTransition = null;
                     
                    
                    public MessageBox(String message, double size){
                        Circle circle = new Circle(size);
                   //     circle.setFill(gradient.build());
                        
                        
                        Text text = new Text(message);
                        text.setStyle("-fx-text-fill: #FFFFFF;-fx-font-size:26pt;");
                 
                        StackPane pane = new StackPane();
                        pane.getChildren().add(circle);
                        pane.getChildren().add(text);
                        getChildren().add(pane);
                 
                        fadeTransition = new FadeTransition();
                        fadeTransition.setNode(circle);
                        fadeTransition.setFromValue(1.0);
                        fadeTransition.setToValue(0.3);
                        fadeTransition.setCycleCount(6);
                        fadeTransition.setAutoReverse(true);
                        fadeTransition.setDuration(Duration.valueOf(2000));
                        
                        visibleProperty().addListener(new ChangeListener() {
                 
                           @Override
                            public void changed(ObservableValue value,
                                    Object arg1, Object arg2) {
                        
                                if (value.getValue().equals(Boolean.TRUE)){
                                    fadeTransition.play();    
                                  System.out.println("Notification: Composition is visible");
                                }
                                
                            }
                 
                        });  
                 
                    }}
                MSBox.java
                import javafx.application.Application;
                import javafx.event.ActionEvent;
                import javafx.event.EventHandler;
                import javafx.scene.Group;
                import javafx.scene.Scene;
                import javafx.scene.control.Button;
                import javafx.scene.paint.Color;
                import javafx.stage.Stage;
                
                
                public class MSBox extends Application {
                
                    public static void main(String[] args) {
                        Application.launch(MSBox.class, args);
                    }
                
                    @Override
                    public void start(Stage primaryStage) {
                        primaryStage.setTitle("Hello World");
                        Group root = new Group();
                        Scene scene = new Scene(root, 350, 250, Color.LIGHTGREEN);
                        Button btn = new Button();
                        Button btn2 = new Button("Invisible");
                
                        btn.setLayoutX(150);
                        btn.setLayoutY(180);
                
                        btn2.setLayoutX(250);
                        btn2.setLayoutY(180);
                
                        int a = 0;
                        btn.setText("Visible");
                        final MessageBox msbox = new MessageBox("Hello", 60.00);
                        msbox.setVisible(false);
                        btn.setOnAction(new EventHandler<ActionEvent>() {
                
                            public void handle(ActionEvent event) {
                
                                msbox.setVisible(true);
                
                
                            }
                        });
                
                        btn2.setOnAction(new EventHandler<ActionEvent>() {
                
                            public void handle(ActionEvent event) {
                
                
                                msbox.setVisible(false);
                
                            }
                        });
                
                
                
                        root.getChildren().addAll(btn, btn2, msbox);
                        primaryStage.setScene(scene);
                        primaryStage.setVisible(true);
                    }
                }
                • 5. Re: Notification about Node visibility
                  Zlaja
                  It would work but I want to stop animation when node is not visible.

                  Zlaja
                  • 6. Re: Notification about Node visibility
                    Zlaja
                    It helps me. The problem was initial setting of visibility. I set it to false in constructor of composition node and it works fine now.