简体   繁体   中英

How to ask pane to re-layout

It seems that in Javafx2 when you add items to a pane, then make one invisible it still takes up space in the layout. Does anyone know how to ask the pane to adjust it's layout after a child is made invisible?

Below is a sample program that demonstrates my issue. There are 3 buttons in a VBox. When you click on the top or middle button it become invisible, but it leaves a gap.

public class VisibilityTest extends Application {

Button button1 = null;
Button button2 = null;
Button button3 = null;
VBox   box     = null;

@Override
public void start(Stage primaryStage) {
    button1 = new Button("Button 1");
    button2 = new Button("Button 2");
    button3 = new Button("Button 3");

    box = new VBox();
    box.getChildren().add(button1);
    box.getChildren().add(button2);
    box.getChildren().add(button3);
    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button1.setVisible(false);
        }
    });
    button2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button2.setVisible(false);
        }
    });
    button3.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button3.setVisible(false);
        }
    });
    Scene scene = new Scene(box, 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

调用更高级别组件的validate()

您需要从场景图中删除控件。

parent.getChildren().remove(theControl);

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