Skip to content
Advertisement

GridPane: How to position a Label top-left?

How can I position the Label Label 1 at the top-left position of the cell? I tried to set the alignment of the label accordingly, but this didn’t do the trick.

enter image description here

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane hgap="14.0" vgap="7.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity" prefWidth="400.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
   <children>
      <Label text="Label 1" />
      <VBox spacing="4.0" GridPane.columnIndex="2">
         <children>
            <HBox spacing="4.0">
               <children>
                  <TextField styleClass="" HBox.hgrow="ALWAYS">
                     <tooltip>
                        <Tooltip style="-fx-font-size: 100%;" text="Subject 1; Subject 2; Subject 3" />
                     </tooltip>
                  </TextField>
                  <Button mnemonicParsing="false" text="Add" />
               </children>
            </HBox>
            <ScrollPane hbarPolicy="NEVER" maxHeight="-Infinity" prefHeight="61.0" />
         </children>
      </VBox>
      <Label text="Label 2" GridPane.rowIndex="1" />
      <CheckBox mnemonicParsing="false" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <HBox alignment="CENTER_LEFT" spacing="4.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
         <children>
            <TextField HBox.hgrow="ALWAYS" />
            <Hyperlink text="duplicate" />
         </children>
      </HBox>
   </children>
</GridPane>

d

Advertisement

Answer

You need to modify the first RowConstraints and set the valignment property to VPos.TOP.

The vertical alignment for the row. If set, will be the default vertical alignment for nodes contained within the row. If this property is set to VPos.BASELINE, then the fillHeight property will be ignored and nodes will always be resized to their preferred heights (docs).

Like this:

[...]
<rowConstraints>
   <RowConstraints valignment="TOP" vgrow="SOMETIMES" /> <!-- added valignment="TOP" -->
   <RowConstraints vgrow="SOMETIMES" />
   <RowConstraints />
   <RowConstraints />
</rowConstraints>
[...]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement