JavaFX - GridPane与DatePicker的奇怪行为

时间:2015-07-01 00:58:23

标签: java layout javafx datepicker gridpane

包含DatePickers时,GridPane似乎无法正确调整大小。任何人都知道发生了什么或潜在的解决方案?我已经尝试了许多其他节点(CheckBox,ChoiceBox等),而DatePicker似乎是唯一导致此行为的节点。想法?

编辑:ColorPicker和ComboBox似乎都很好,尽管也像DatePicker一样扩展了ComboBoxBase。

@Override
public void start(Stage primaryStage) {
  HBox goodBox = new HBox();
  goodBox.setStyle("-fx-border-color: green");
  goodBox.setPadding(new Insets(5));
  GridPane goodGrid = new GridPane();
  goodGrid.setHgap(5);
  goodGrid.setVgap(5);
  goodGrid.addColumn(0, new Label("Good 1:"), new Label("Good 2:"));
  goodGrid.addColumn(1, new TextField(), new TextField());
  goodBox.getChildren().add(goodGrid);

  HBox badBox = new HBox();
  badBox.setStyle("-fx-border-color: red");
  badBox.setPadding(new Insets(5));
  GridPane badGrid = new GridPane();
  badGrid.setHgap(5);
  badGrid.setVgap(5);
  badGrid.addColumn(0, new Label("Bad 2:"), new Label("Bad 2:"));
  badGrid.addColumn(1, new DatePicker(), new DatePicker());
  badBox.getChildren().add(badGrid);

  VBox root = new VBox(5);
  root.setAlignment(Pos.TOP_LEFT);
  root.setPadding(new Insets(5));
  root.getChildren().addAll(goodBox, badBox);

  Scene scene = new Scene(root);
  primaryStage.setScene(scene);
  primaryStage.show();
}

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

Example

1 个答案:

答案 0 :(得分:0)

在设置场景并显示舞台时添加以下内容似乎有效:

Scene scene = new Scene(root);
primaryStage.setScene(scene);
root.applyCss();
root.layout();
primaryStage.show();

我很想知道为什么这是必要的。