Method hide_old_show_new (I can't think of a suitable name) gradually, using FadeTransition, removes all nodes (from last to first) and then adds new nodes (from first to last)
But, it is unnecessarily complex.
Do you know how to simplify it? Or a better algorithm? I'll be grateful for the improvement.
However, I do not want to use an algorithm using *.fxml.
import java.util.ArrayList;
import javafx.animation.FadeTransition;
import javafx.scene.text.Text;
import javafx.util.Duration;
public final class Cart extends javafx.scene.layout.VBox {
public Cart(double x, double y, double width, double height) {
super();
setLayoutX(x);
setLayoutY(y);
setWidth(width);
setHeight(height);
}
public FadeTransition hide_old_show_new(ArrayList<Text> nodes) {
var transition = new FadeTransition();
var size = getChildren().size();
if (size > 0 && !getChildren().get(0).equals(nodes.get(0))) {
transition = new FadeTransition(Duration.millis(200), getChildren().get(size - 1));
transition.setFromValue(1);
transition.setToValue(0);
transition.setOnFinished((event) -> {
getChildren().remove(size - 1);
hide_old_show_new(nodes);
});
transition.play();
} else if (size < nodes.size()) {
getChildren().add(nodes.get(size));
getChildren().get(size).setOpacity(0);
transition = new FadeTransition(Duration.millis(200), getChildren().get(size));
transition.setFromValue(0);
transition.setToValue(1);
transition.setOnFinished((event) -> {
hide_old_show_new(nodes);
});
transition.play();
}
return transition;
}
}