5

I have a problem that has been driving me nuts for a couple of days.

I have a GridPane and I want to hide the first row when I click on a button.
This is the FXML file

<VBox prefHeight="200.0" prefWidth="100.0">
<children>
 <Button fx:id="buttonTest" mnemonicParsing="false" onAction="#handleButtonTestAction" text="Button" />
<GridPane fx:id="gridPaneTest" gridLinesVisible="true" layoutX="0.5" layoutY="0.5" BorderPane.alignment="CENTER">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
  <children>
   <Label fx:id="labelTopLeft" text="top left">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
   <Label fx:id="labelTopRight" text="top right" GridPane.columnIndex="1">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
   <Label text="center left" GridPane.rowIndex="1">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
   <Label text="center right" GridPane.columnIndex="1" GridPane.rowIndex="1">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
   <Label text="bottom left" GridPane.rowIndex="2">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
   <Label text="bottom right" GridPane.columnIndex="1" GridPane.rowIndex="2">
    <font>
     <Font size="15.0" />
    </font>
   </Label>
  </children>
 </GridPane>
</children>
</VBox>

If I click on the Button I do this

@FXML
public void handleButtonTestAction() {

    labelTopLeft.setVisible(false);
    labelTopRight.setVisible(false);

    gridPaneTest.getRowConstraints().get(0).setMinHeight(0);
    gridPaneTest.getRowConstraints().get(0).setPrefHeight(0);
    gridPaneTest.getRowConstraints().get(0).setMaxHeight(0);
}

After I click the Button, the labels are invisible as expected but the height of the first row doesn't change at all. Do I have to update the GridPane after I change the row constraints or is there anything else to do?

THANK YOU!

stefOCDP
  • 803
  • 2
  • 12
  • 20

1 Answers1

6

Node.setVisible() just toggles the visibility state of a Node.

To exclude a Node from its parents layout calculations you additionally have to set its managed state, by calling Node.setManaged(false).

eckig
  • 10,964
  • 4
  • 38
  • 52
  • Thank you very much, that works fine. Is it possible to make it visible again. Because just Node.setManaged(true) and Node.serVisible(true) doesn't work. – stefOCDP Feb 04 '15 at 10:07
  • Yes, `setVisibible(true)` and `setManaged(true)` should make it re-appear. – eckig Feb 04 '15 at 10:13
  • Just had to say thanks for this. I don't know if I ever would have found `Node.setManaged()`. – Dominic P Jan 12 '17 at 23:55