如何在JavaFx中的GridPane中添加空行?

时间:2012-08-13 12:10:00

标签: javafx

我想在循环中每x行的行之间添加一点空格。我发现将空行添加到GridPane比在行上设置特定约束更好。问题是我不知道应该在哪一个节点放入假行空元素。我可以通过放开说文本节点来做。但这真的是对的吗?任何人都可以提供更优雅的解决方案吗?

gridPane.addRow(i, new Text(""));

3 个答案:

答案 0 :(得分:7)

使用带有空字符串的Text节点来创建空的gridpane行很好。

作为替代方案,下面的示例使用窗格为空网格行创建“弹簧”节点,可以将其首选高度设置为任何所需值,以实现所需的任何间隙大小。另外,如果需要,也可以通过css设置spring节点的样式。

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();
  }
}

答案 1 :(得分:3)

我认为解决此问题的最佳方法是添加RowConstraints,设置Gridpane中每行的高度。然后你不必添加“空”行,因为每行都将获得相同的空间,无论它是否包含任何内容。

这是一个minimal, complete and verifiable示例:

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();
    }
}

这种方法的问题在于 - 据我所知 - 没有简单的方法来获取GridPane中的行数量,并且没有简单的方法可以将相同的RowConstraint添加到{{{ 1}}。这使得代码相当混乱。但你可以通过例如为GridPane创建自己的子类,以跟踪大小。

在上面的示例中,我们设置行的pref高度,但如果您不知道每个标签需要多少空间,也可以使用.setPercentHeight(double)。

答案 2 :(得分:-6)

GridPane gp = new GridPane();
gp.add(" ",2, 2);
相关问题