简体   繁体   中英

JavaFX StackPane bounds not updated

When I run the following program

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;


public class Test1 extends Application {

    public static void main(String[] args) throws Exception {       
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {       
        Pane topPane = new Pane();      
        Scene scene = new Scene(topPane, 600, 400);

        StackPane sp = new StackPane();
        Label l1 = new Label("1 2");
        Ellipse e1 = new Ellipse(100, 50);
        e1.setOpacity(0.5);
        sp.getChildren().addAll(l1, e1);            
        e1.radiusXProperty().bind(l1.widthProperty());
        e1.radiusYProperty().bind(l1.heightProperty());         
        topPane.getChildren().add(sp);
        sp.relocate(200, 100);

        sp.setStyle("-fx-border-color: RED;");              

        Platform.runLater(() -> {
            //l1.setText("123");
            //l1.setText("1 2");
        });             

        stage.setScene(scene);
        stage.show();       
    }
}

I get a red box surrounding the text label only, but when I uncomment the two lines inside the Platform.runLater() block above, I get a red box surrounding the outer ellipse, which is what I want. So it seems to me the layout bounds of the stack pane is not set correctly from the model description, since the bounds are determined only from the label control. But when I force an invalidation in Platform.runLater() the layout bounds are where they should be.
Why is this happening and how do I prevent it? I would like to be able to just specify my model/graph and then it should render correctly on the first show, or?

add this sp.requestLayout(); after stage.Show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM