对话框内容不会占据对话框的整个宽度

时间:2019-01-31 18:37:20

标签: java javafx javafx-8 alert

我有一个JavaFX对话框,看来网格的内容没有跨越对话框的整个宽度。现在,它跨越到对话框的左侧最大,到OK按钮的宽度。我想删除输入字段右侧的空白。或跨越整个宽度。

我尝试过:

  • 设定gridpane的最小宽度

  • 设置TextField的最小宽度

  • 设置舞台的宽度

我在这里很茫然。

这是我的代码!

// Create the custom dialog.
Dialog dialog = new Dialog<>();

dialog.initOwner(mainStage);

// Set Custom Icon
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(Constants.kApplicationIcon));

dialog.getDialogPane().getStylesheets().add(Constants.kRootStylesheet);
dialog.getDialogPane().getStyleClass().add("accountDialog");

dialog.setTitle("New User Detected");
dialog.setHeaderText("Please complete user registration!");

// Set the button types.
ButtonType loginButtonType = new ButtonType("Create Account", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);

// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
grid.setAlignment(Pos.CENTER);
grid.setId("accountGrid");

TextField firstName = new TextField("First Name");
TextField lastName = new TextField("Last Name");

TextField email = new TextField("Email");
TextField gender = new TextField("Gender");

firstName.setId("textField");
lastName.setId("textField");

firstName.setPromptText("");
lastName.setPromptText("");

gender.setPromptText("");
email.setPromptText("");

email.setId("textField");
gender.setId("textField");

ToggleGroup studentRadioGroup = new ToggleGroup();

RadioButton mentorRadio = new RadioButton("Mentor");
RadioButton studentRadio = new RadioButton("Student");

studentRadio.fire();

mentorRadio.setToggleGroup(studentRadioGroup);
studentRadio.setToggleGroup(studentRadioGroup);

grid.add(new Label("First Name:"), 0, 0);
grid.add(firstName, 1, 0);
grid.add(new Label("Last Name:"), 0, 1);
grid.add(lastName, 1, 1);
grid.add(new Label("Email:"), 0, 2);
grid.add(email, 1, 2);
grid.add(new Label("Gender:"), 0, 3);
grid.add(gender, 1, 3);

GridPane.setHalignment(grid, HPos.CENTER);

GridPane.setHalignment(studentRadio, HPos.CENTER);
GridPane.setHalignment(studentRadio, HPos.CENTER);

grid.add(studentRadio, 0, 4);
grid.add(mentorRadio, 1, 4);

grid.setGridLinesVisible(true);

// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);

// Do some validation (using the Java 8 lambda syntax).
firstName.textProperty().addListener((observable, oldValue, newValue) -> {
    loginButton.setDisable(newValue.trim().isEmpty());
});

dialog.getDialogPane().setContent(grid);

// Request focus on the firstname field by default.
Platform.runLater(firstName::requestFocus);

Optional<ButtonType> result = dialog.showAndWait();
ArrayList<String> data = new ArrayList<>();

System.out.println(result.get().toString());

if (result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE) {
    data.add("TRUE");
    data.add(firstName.getText());
    data.add(lastName.getText());
    data.add(email.getText());
    data.add(gender.getText());
    data.add(mentorRadio.isSelected() ? "TRUE" : "FALSE");

} else {
    data.add("FALSE");

}

return data; 

这里的结果的图像。我想再次;要么删除网格右侧的所有空白,要么跨网格以适合整个宽度。

Image of Result

3 个答案:

答案 0 :(得分:3)

您正在使用grid.setPadding(new Insets(20, 150, 10, 10));创建多余的空间。 150是正确的填充。参见the Insets documentation

答案 1 :(得分:0)

您是否尝试过对话框窗格中的setExpandableContent()方法?您可以传递任何您喜欢的节点,然后它应该能够调整大小以适合整个警报。

将GridPane设置为警报内容的示例。

GridPane() grid = new GridPane();
Alert alert = new Alert(AlertType.INFORMATION);
alert.getDialogPane().setExpandableContent(grid);

答案 2 :(得分:0)

GridPane的大小已调整为首选大小。 150右边的填充物可防止TextField在此方向上变大。

grid.setPadding(new Insets(20, 150, 10, 10)); // sets right padding to 150

如果要将TextField s增加到左侧叶子{{1} ss / Label的最大大小,则应使用RadioButton s来指定第二列应该总是增长:

ColumnConstraint

还可以通过ColumnConstraints constraints = new ColumnConstraints(); constraints.setHgrow(Priority.ALWAYS); grid.getColumnConstraints().addAll(new ColumnConstraints(), constraints); 对象指定第二列的首选宽度。

要测试窗口大小更改时的行为,可以使对话框可调整大小:

ColumnConstraints

要查看dialog.setResizable(true); 覆盖的区域,可以指定背景:

GridPane