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

标签 java user-interface javafx gridpane

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

Sign Up Dialog

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

我在对话框上使用 GridPane,我正在考虑在这些单元格返回某些节点(检查 ImageView)并检查条件是否为真。但是,我不知道如何从 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();


}

最佳答案

创建适当的 BooleanBinding它表示何时应该禁用按钮。您可以使用 Bindings用于创建表达式的实用程序类,包括比较、ands 和 ors。为了使代码更具可读性,请静态导入函数。

从面板中获取创建按钮并将 boolean 表达式绑定(bind)到 disable你的按钮的属性。

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

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

您可以使用相同的机制来控制每个复选标记的可见性。为每个文本字段创建一个“不完整”BooleanBinding 并将其与 not 绑定(bind)。绑定(bind)到 visible复选标记的属性。在复合 or 中使用所有这些 BooleanBindings 来确定按钮状态。这样按钮状态和复选标记将始终保持同步。

关于java - 从 GridPane 上的单元格返回节点。 (JavaFX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560529/

相关文章:

user-interface - UI设计建议

java - 为具有预加载器的 JavaFX 应用程序创建 native 包

java - Runnable 和 ScheduledExecutorService 的内存泄漏

JavaFX 测试应用程序是作为一个整体关闭还是只是单个窗口关闭

java - 如何将 swing jButton 上的文本字体大小设置为适合 jButton 的最大大小

java - 从 Java GUI 检索多个文件

java - DefaultFormatter的覆盖模式描述不清楚

java - 单击鼠标移动一个圆圈

java - 仅对连接字符串的一部分进行样式设置 (Android)

java - Hibernate - 从单向 OneToMany 关系中获取集合的 HQL