从 GridPane 上的单元格返回节点。 (JavaFX)

Returning a node from a cell on GridPane. (JavaFX)

我有一个学校项目或类似的项目,我正在尝试为用户制作一个注册面板。当用户点击注册时,该面板打开。看起来像这样。

我想做的是禁用该创建按钮,并且仅当对话框中有 3 个检查时才会启用它。

我在 Dialog 上使用 GridPane,我正在考虑 return 在这些单元格处设置那些特定节点(检查它们是 ImageView)并检查条件是否为真。但是,我不知道如何 return 来自 GridPane 的节点。如果您对这个问题有任何其他方法,那也很好。

这是代码的相关部分。

public void SignUp(){

    //Create the custom dialog.
    Dialog signUpDialog = new Dialog();
    //Dialog Title
    signUpDialog.setTitle("Sign Up");

    //Setting "OK" button type.
    ButtonType buttonTypeCreate = new ButtonType("Create", ButtonBar.ButtonData.OK_DONE);
    //Adding Button types.
    signUpDialog.getDialogPane().getButtonTypes().addAll(buttonTypeCreate, ButtonType.CANCEL);

    //Creating the GridPane.
    GridPane gridPane = new GridPane();
    gridPane.setHgap(10);
    gridPane.setVgap(10);
    gridPane.setPadding(new Insets(20, 150, 10, 10));

    //Setting the Check Icon.
    Image imageCheck = new Image("resources/check_icon.png");
    //Setting 3 different ImageViews for Check Icon because can't add duplicates to GridPane.
    ImageView imageViewCheck1 = new ImageView(imageCheck);
    ImageView imageViewCheck2 = new ImageView(imageCheck);
    ImageView imageViewCheck3 = new ImageView(imageCheck);

    //Setting the X Icon.
    Image imageX = new Image("resources/x_icon.png");
    //Setting 3 different ImageViews for X Icon because can't add duplicates to GridPane.
    ImageView imageViewX1 = new ImageView(imageX);
    ImageView imageViewX2 = new ImageView(imageX);
    ImageView imageViewX3 = new ImageView(imageX);

    //TextField for User ID.
    TextField textFieldDialogUserID = new TextField();
    textFieldDialogUserID.setPromptText("User ID");
    textFieldDialogUserID.setAlignment(Pos.CENTER_RIGHT);

    //PasswordField for Password.
    PasswordField passwordFieldDialogPassword = new PasswordField();
    passwordFieldDialogPassword.setPromptText("Password");
    passwordFieldDialogPassword.setAlignment(Pos.CENTER_RIGHT);

    //PasswordField for Confirm Password.
    PasswordField passwordFieldDialogConfirmPassword = new PasswordField();
    passwordFieldDialogConfirmPassword.setPromptText("Confirm Password");
    passwordFieldDialogConfirmPassword.setAlignment(Pos.CENTER_RIGHT);

    gridPane.add(new Label("User ID"), 0, 0);
    gridPane.add(textFieldDialogUserID, 1, 0);

    gridPane.add(new Label("Password"), 0, 1);
    gridPane.add(passwordFieldDialogPassword, 1, 1);

    gridPane.add(new Label("Confirm Password"), 0, 2);
    gridPane.add(passwordFieldDialogConfirmPassword, 1, 2);

    gridPane.add(imageViewX1,2,0);
    gridPane.add(imageViewX2,2,1);
    gridPane.add(imageViewX3,2,2);


    signUpDialog.getDialogPane().setContent(gridPane);

    Stage signUpStage = (Stage) signUpDialog.getDialogPane().getScene().getWindow();
    signUpStage.getIcons().add(new Image("resources/application_icon.png"));

    Optional<Pair<String, String>> result = signUpDialog.showAndWait();


}

I could not figure out how to return a node from GridPane.

    gridPane.getChildren() 

提供了节点列表,但您已经拥有组件 textFieldDialogUserIDpasswordFieldDialogPasswordpasswordFieldDialogConfirmPassword

=> 为它们中的每一个添加一个动作监听器,当它的值改变时检查值。根据结果​​,enable/disable 创建按钮(默认情况下,它应该被禁用)。

你可以举个例子:http://docs.oracle.com/javafx/2/ui_controls/text-field.htm

创建适当的 BooleanBinding which expresses when the button should be disabled. You can use the Bindings 实用程序 class 来创建表达式,包括比较、ands 和 ors。为了使代码更具可读性,请对函数进行静态导入。

从面板获取创建按钮并将布尔表达式绑定到按钮的 disable 属性。

如果任何值发生更改,JavaFX 框架将自动重新评估绑定并相应地更新按钮的状态。

import static javafx.beans.binding.Bindings.*;

BooleanBinding notComplete = or(
  equal(textFieldDialogUserID.textProperty(), null),
  equal(passwordFieldDialogPassword.textProperty(), null));

Node createButton = signUpDialog.getDialogPane().lookupButton(buttonTypeCreate);
createButton.disableProperty().bind(notComplete);

您可以使用相同的机制来控制每个复选标记的可见性。为每个文本字段创建一个 'incomplete' BooleanBinding 并将其与复选标记的 not binding to the visible 属性 绑定。在复合 or 中使用所有这些 BooleanBindings 来确定按钮状态。这样按钮状态和复选标记将始终同步。