9 Replies Latest reply: Jan 18, 2013 3:49 AM by bape_tc RSS

    Blinking Button

    bape_tc
      How can I create a blinking button in JavaFX?
        • 1. Re: Blinking Button
          984080
          Check those examples:

          http://fxexperience.com/2011/12/styling-fx-buttons-with-css/
          • 2. Re: Blinking Button
            bape_tc
            Thx, I can only find styling, but no blinking.
            • 3. Re: Blinking Button
              956927
              Hy,

              use a timer thread for your issue and modify the style within it:

              boolean toggle = true;
              new Timer().schedule(
              new TimerTask() {
              @Override
              public void run() {
              //toggle Stylinge here
              if(toggle)
              {
              myButton.setStyle("fx-background-color:#666666");
              toggle = false;
              }
              else{
              myButton.setStyle("fx-background-color:#333333");
              toggle = true;
              }
              }
              }, 0, 5000); //5seconds interval (5000ms)
              • 4. Re: Blinking Button
                shakir.gusaroff
                Hi. Here is another example using a fadeTransition:
                 @Override
                    public void start(Stage primaryStage) {
                        final Button btn = new Button();
                        btn.setText("Submit");
                
                
                
                        FadeTransition ft = new FadeTransition(Duration.millis(500), btn);
                        ft.setFromValue(1.0);
                        ft.setToValue(0.3);
                        ft.setCycleCount(Animation.INDEFINITE);
                        ft.setAutoReverse(true);
                
                        ft.play();
                
                        StackPane root = new StackPane();
                        root.getChildren().add(btn);
                
                        Scene scene = new Scene(root, 300, 250);
                
                        primaryStage.setTitle("Hello World!");
                        primaryStage.setScene(scene);
                        primaryStage.show();
                    }
                • 5. Re: Blinking Button
                  biochemistry43
                  I did a similar effect in this way...

                  First I create a dropShadow a timeline animation... then create a timeline object
                  DropShadow ds = new DropShadow();
                         
                         Timeline timeLine; 
                         
                         ds.setColor(Color.web("#808080"));
                         ds.setRadius(0.1);
                         ds.setSpread(0.6f);
                         ds.setOffsetX(0.0f);
                         ds.setOffsetY(0.0f);
                          
                         timeLine= new Timeline();
                         timeLine.setCycleCount(Timeline.INDEFINITE);
                         timeLine.setAutoReverse(true);
                      
                         KeyValue keyvalue1 =  new KeyValue(ds.radiusProperty(), 2, Interpolator.EASE_OUT);
                         ds.setRadius(20.0);
                         KeyValue keyvalue2 =  new KeyValue(ds.radiusProperty(), 20, Interpolator.EASE_OUT);
                      
                         Duration duration = Duration.millis(800);
                       
                         KeyFrame keyframe1 = new KeyFrame(Duration.seconds(0), keyvalue1);
                         KeyFrame keyframe2 = new KeyFrame(duration, keyvalue2);
                      
                         timeLine.getKeyFrames().add(keyframe1);
                         timeLine.getKeyFrames().add(keyframe2);
                       
                         timeLine.play();
                  Then ... depends on the event that you want to manage, you pass the shadow effect
                        button.setEffect(ds);
                  Hope that helps
                  • 6. Re: Blinking Button
                    biochemistry43
                    A question ... components are creating with Java code? or FXML code?
                    • 7. Re: Blinking Button
                      bouye
                      Can be both.
                      • 8. Re: Blinking Button
                        biochemistry43
                        well ... I did with FXML as follows:

                                    <ToggleButton fx:id="addUnitBtn" onAction="#switchPane" onMouseEntered="#effectButtons" onMouseExited="#effectButtonNull" text="${'Add' + '\n' + 'Unit'}" toggleGroup="$Panels" />
                                    <ToggleButton fx:id="defeatBtn" onAction="#switchPane" onMouseEntered="#effectButtons" onMouseExited="#effectButtonNull" text="Defeat" toggleGroup="$Panels" />
                                    <ToggleButton fx:id="editUnitBtn" onAction="#switchPane" onMouseEntered="#effectButtons" onMouseExited="#effectButtonNull" text="${'Edit' + '\n' + 'Units'}" toggleGroup="$Panels" />
                        You see, the button event I passed the methods (effectButtons and effectButtonNull)

                        Now ... code method (full) is as follows
                        public void effectButtons (MouseEvent event)
                            {
                               DropShadow ds = new DropShadow(); 
                               Timeline timeLine; 
                               
                               ds.setColor(Color.web("#808080"));
                               ds.setRadius(0.1);
                               ds.setSpread(0.6f);
                               ds.setOffsetX(0.0f);
                               ds.setOffsetY(0.0f);
                                
                               timeLine= new Timeline();
                               timeLine.setCycleCount(Timeline.INDEFINITE);
                               timeLine.setAutoReverse(true);
                            
                               KeyValue keyvalue1 =  new KeyValue(ds.radiusProperty(), 2, Interpolator.EASE_OUT);
                               ds.setRadius(20.0);
                               KeyValue keyvalue2 =  new KeyValue(ds.radiusProperty(), 20, Interpolator.EASE_OUT);
                            
                               Duration duration = Duration.millis(800); //tiempo que dura la animación
                             
                               KeyFrame keyframe1 = new KeyFrame(Duration.seconds(0), keyvalue1);
                               KeyFrame keyframe2 = new KeyFrame(duration, keyvalue2);
                            
                               timeLine.getKeyFrames().add(keyframe1);
                               timeLine.getKeyFrames().add(keyframe2);
                             
                               timeLine.play(); 
                               
                               Object eventSource = event.getSource();
                                        if (eventSource == addUnitBtn) {
                                       addUnit.setEffect(ds);
                                                                   
                                  } else if (eventSource == defeatBtn) {
                                                defeatBtn.setEffect(ds);
                        
                                  }else if (eventSource == editUnitBtn) {
                                               editUnitBtn.setEffect(ds);
                        
                                  } 
                            }
                        
                        public void effectButtonNull(MouseEvent event) {
                              Object eventSource = event.getSource();
                                        if (eventSource == addUnitBtn) {
                                       addUnitBtn.setEffect(null);
                                                                   
                                  } else if (eventSource == defeatBtn) {
                                                defeatBtn.setEffect(null);
                        
                                  }else if (eventSource == editUnitBtn) {
                                               editUnitBtn.setEffect(null);
                        
                                  }
                        }
                        • 9. Re: Blinking Button
                          bape_tc
                          Thanks for the help!