How to fade in a hidden JavaFX label


Question

How can I add fancy JavaFX events on hidden items?

For example, I'd like to use the "Fade" effect when I show a hidden Label, triggered by button click.

How can I add the effect when the method below is called?

@FXML
private void handleButtonAction(ActionEvent event) {
    label.setVisible(true);
}
1
3
2/23/2017 5:07:51 PM

Accepted Answer

Use a FadeTransition:

@FXML private Label label;

private FadeTransition fadeIn = new FadeTransition(
    Duration.millis(3000)
);

public void initialize() {
    fadeIn.setNode(label);

    fadeIn.setFromValue(0.0);
    fadeIn.setToValue(1.0);
    fadeIn.setCycleCount(1);
    fadeIn.setAutoReverse(false);
}

@FXML
private void handleButtonAction(ActionEvent event) {
    if (!label.isVisible()) {
        label.setVisible(true);
        fadeIn.playFromStart();
    }
}
7
9/17/2013 8:36:21 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon