Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX How to change the GridPane row height programmatically

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!

like image 493
stefOCDP Avatar asked Feb 04 '15 09:02

stefOCDP


1 Answers

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).

like image 117
eckig Avatar answered Nov 15 '22 09:11

eckig