0

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();
}
}
phil-daniels
  • 574
  • 2
  • 10
  • 28
  • I just read in the VBox javadoc `VBox lays out each managed child regardless of the child's visible property value; unmanaged children are ignored.`. It looks like visibility is different in javafx than in swing. Visible items are not ignored in layout. – phil-daniels Oct 27 '12 at 22:01
  • Yes see this related [http://stackoverflow.com/q/12200195/682495](http://stackoverflow.com/q/12200195/682495) – Uluk Biy Oct 28 '12 at 11:43

2 Answers2

1

Call validate() of the higher level component.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

You need to remove the control from scene graph.

parent.getChildren().remove(theControl);
amru
  • 1,388
  • 11
  • 14