JavaFX Marquee go out of my node

2020-03-06 03:20发布

问题:

I have a issue with my Marquee animation with JavaFX. I have a HBox with three Nodes and in the second node I have a Text node inside that I need do the Marquee transformation, but when the text goes out of the second node I need it doesn't be visible.

I'll go to set a picture to show my issue (the text is visible in the white area).

My Hbox code:

    HBox bill = new HBox(0);
    bill.getChildren().addAll(logoPane,product,total);
    bill.setBackground(new Background(new BackgroundFill(Color.web("#FFFFFF"), CornerRadii.EMPTY, Insets.EMPTY)));
    bill.setHgrow(product, Priority.ALWAYS);

Animation:

    timelineAnimation = new Timeline();
    final KeyValue kv = new KeyValue(productLabel.translateXProperty(), -1000);
    final KeyFrame kf = new KeyFrame(Duration.millis(2000), kv);
    timelineAnimation.getKeyFrames().add(kf);

And how I define my product node:

productLabel.setFont(new Font("Times New Roman",30));

    product = new StackPane();
    product.setMaxWidth(2000);
    product.setMaxHeight(100);
    product.setMinWidth(574);
    product.setMinHeight(100);

    product.getChildren().add(productLabel);
    product.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
    product.setAlignment(productLabel, Pos.CENTER);

Hope it was enough information.

Thanks!

回答1:

Simply add a Rectangle as clip for the product pane and bind it's size to the size of the pane:

Rectangle clip = new Rectangle();
product.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
    clip.setWidth(newValue.getWidth());
    clip.setHeight(newValue.getHeight());
});
product.setClip(clip);

This will make sure no descendants of product are drawn outside the bounds of this node.