Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add empty row in GridPane in JavaFx?

Tags:

javafx

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(""));
like image 400
Kamil Avatar asked Aug 13 '12 12:08

Kamil


People also ask

How do you add a row to GridPane in Scene Builder?

This feature exists in the JavaFX Scene Builder by right clicking on the GridPane and selecting GridPane, then it will show a list of options: Add Row Above, Add Row Below, Add Column After, Add Column Before.

How do I delete a row from GridPane in JavaFX?

To remove nodes, just use gridpane. getChildren(). remove(...); or gridpane. getChildren().

How does GridPane work in JavaFX?

GridPane lays out its children within a flexible grid of rows and columns. If a border and/or padding is set, then its content will be layed out within those insets. A child may be placed anywhere within the grid and may span multiple rows/columns.

How do I declare a GridPane in JavaFX?

To make a JavaFX GridPane visible you must add it to the JavaFX scene graph. To do so you must add the GridPane instance to a Scene object, or add the GridPane to a layout component which is added to a Scene object.


2 Answers

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();
  }
}
like image 97
jewelsea Avatar answered Sep 30 '22 08:09

jewelsea


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.

like image 39
Jonatan Stenbacka Avatar answered Sep 30 '22 08:09

Jonatan Stenbacka