This discussion is archived
1 Reply Latest reply: Sep 4, 2013 11:27 AM by MoC RSS

How to set the background colour of a column or row in a grid pane?

abg Newbie
Currently Being Moderated

Hi

 

I have a dialog with a grid pane layout.  Can I set the background colour of individual columns or rows?

 

thanks

 

Alex

  • 1. Re: How to set the background colour of a column or row in a grid pane?
    MoC Explorer
    Currently Being Moderated

    Don't think you can. One trick is to place a styled region in the first cell of the column and and set its row span to remaining. Here's a little FXML mockup I made in Scene Builder.

     

    <?xml version="1.0" encoding="UTF-8"?>
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.paint.*?>
    <GridPane style="-fx-border-color: black;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
      <children>
        <Label text="Name" GridPane.columnIndex="0" GridPane.rowIndex="0">
          <GridPane.margin>
            <Insets left="5.0" fx:id="x1" />
          </GridPane.margin>
        </Label>
        <Label text="Surname" GridPane.columnIndex="0" GridPane.margin="$x1" GridPane.rowIndex="1" />
        <Region prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: darkgrey;" GridPane.columnIndex="1" GridPane.rowIndex="0" GridPane.rowSpan="2147483647" />
        <TextField GridPane.columnIndex="1" GridPane.rowIndex="0">
          <GridPane.margin>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x2" />
          </GridPane.margin>
        </TextField>
        <TextField GridPane.columnIndex="1" GridPane.margin="$x2" GridPane.rowIndex="1" />
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="99.0" minWidth="10.0" prefWidth="75.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="129.0" minWidth="10.0" prefWidth="125.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>

Legend

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