This discussion is archived
3 Replies Latest reply: Jan 25, 2013 8:52 AM by 127223 RSS

GridPane FXML Optional Layout Constraints

127223 Newbie
Currently Being Moderated
Hi
I am trying to set columnConstraints on a GridPane in FXML. It works fine for 'hgrow' and 'halignment', but it fails for 'margin'.
I am doing this:
  <columnConstraints>
    <ColumnConstraints hgrow="NEVER" halignment="RIGHT" >
     <margin>
      <Insets bottom="0.0" left="10.0" right="5.0" top="0.0" />
    </margin>
   </ColumnConstraints>
    <ColumnConstraints hgrow="ALWAYS"/>
  </columnConstraints>
But, I am getting an Invalid Property exception.
What am I doing wrong ?
  • 1. Re: GridPane FXML Optional Layout Constraints
    James_D Guru
    Currently Being Moderated
    ColumnConstraints doesn't have a property called "margin". http://docs.oracle.com/javafx/2/api/javafx/scene/layout/ColumnConstraints.html

    In a grid pane, you can set the margin on a cell-by-cell basis using (in Java code) GridPane.setMargin(node, new Insets(...));

    In FXML this translates as
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.ColumnConstraints?>
    <?import javafx.geometry.Insets?>
    
    <GridPane xmlns:fx="http://javafx.com/fxml">
         <columnConstraints>
              <ColumnConstraints hgrow="NEVER" halignment="RIGHT">
    
              </ColumnConstraints>
              <ColumnConstraints hgrow="ALWAYS" />
         </columnConstraints>
         <Label text="First Name">
              <GridPane.rowIndex>0</GridPane.rowIndex>
              <GridPane.columnIndex>0</GridPane.columnIndex>
              <GridPane.margin>
                   <Insets bottom="0.0" left="10.0" right="5.0" top="0.0" />
              </GridPane.margin>
         </Label>
         <Label text="Last Name">
              <GridPane.rowIndex>1</GridPane.rowIndex>
              <GridPane.columnIndex>0</GridPane.columnIndex>
              <GridPane.margin>
                   <Insets bottom="0.0" left="10.0" right="5.0" top="0.0" />
              </GridPane.margin>
         </Label>
         <TextField>
              <GridPane.rowIndex>0</GridPane.rowIndex>
              <GridPane.columnIndex>1</GridPane.columnIndex>
         </TextField>
         <TextField>
              <GridPane.rowIndex>1</GridPane.rowIndex>
              <GridPane.columnIndex>1</GridPane.columnIndex>
         </TextField>
    </GridPane>
  • 2. Re: GridPane FXML Optional Layout Constraints
    984080 Explorer
    Currently Being Moderated
    You can also working with horizontal or vertigal gaps between the rows/columns instead of margins. Cause margins are only set 1 time for the hole grid.

    just by adding this:
    <hgap>
    <Long fx:value="5" />
    </hgap>
    
    <vgap>
    <Long fx:value="5"/>
    </vgap>
  • 3. Re: GridPane FXML Optional Layout Constraints
    127223 Newbie
    Currently Being Moderated
    OK
    I was confused by the GridPane javadoc, didn't look at the ColumnConstraints javadoc
    Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points