How to add empty row in GridPane in JavaFx?


Question

I would like to add little space between rows every x row in the loop. I find this is better to add empty row to GridPane than setting particular constrains on rows. Problem is I don't know what node should I put into that row to fake empty element. I could do by putting let say Text node. But is this really correct? Can anyone provide more elegant solution?

gridPane.addRow(i, new Text(""));
1
3
7/29/2017 10:30:01 PM

Accepted Answer

Using a Text node with an empty string for creating the empty gridpane row is fine.

As an alternative, the sample below uses a Pane to create a "spring" node for the empty grid row which could have it's preferred height set to any required value to achieve whatever gap size you want. Additionally the spring node can also be styled via css if necessary.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

// GridPane with a blank row
// http://stackoverflow.com/questions/11934045/how-to-add-empty-row-in-gridpane-in-javafx
public class GridPaneWithEmptyRowSample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    // create nodes for the grid.
    final Label label1 = new Label("Label 1");
    final Label label2 = new Label("Label 2");
    final Label label3 = new Label("Label 3");
    final Pane  spring = new Pane();
    spring.minHeightProperty().bind(label1.heightProperty());

    // layout the scene.
    final GridPane layout = new GridPane();
    layout.add(label1, 0, 0);
    layout.add(spring, 0, 1);
    layout.add(label2, 0, 2);
    layout.add(label3, 0, 3);
    layout.setPrefHeight(100);
    stage.setScene(new Scene(layout));
    stage.show();
  }
}
7
8/13/2012 6:05:32 PM

I think the best way to solve this is by adding RowConstraints, setting the height of each row in the Gridpane. Then you wont have to add "empty" rows, because each row will acquire the same space regardless of whether it contains anything or not.

Here's a minimal, complete and verifiable example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SSCCE extends Application {

    @Override
    public void start(Stage stage) {

        VBox root = new VBox();

        GridPane gridPane = new GridPane();

        gridPane.add(new Label("First"), 0, 0);
        gridPane.add(new Label("Second"), 0, 2);
        gridPane.add(new Label("Third"), 0, 3);

        // Add one RowConstraint for each row. The problem here is that you
        // have to know how many rows you have in you GridPane to set
        // RowConstraints for all of them.
        for (int i = 0; i <= 3; i++) {
            RowConstraints con = new RowConstraints();
            // Here we set the pref height of the row, but you could also use .setPercentHeight(double) if you don't know much space you will need for each label.
            con.setPrefHeight(20);
            gridPane.getRowConstraints().add(con);
        }

        root.getChildren().add(gridPane);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }
}

The problem with this method is that there's - to my knowledge - no easy way to to get the amount rows in a GridPane, and there's no easy way to add the same RowConstraint to every row in the GridPane. This makes the code rather messy. But you could solve this by e.g. creating your own subclass to GridPane that keeps track of the size.

In the example above we set the pref height of the row, but you could also use .setPercentHeight(double) if you don't know much space you will need for each label.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon