Search code examples
javauser-interfacejavafxgridpane

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


I have a school project or something like that and I am trying to make a sign up panel for users. This panel opens when user clicks on sign up. It looks like this.

Sign Up Dialog

What I am trying to do is I want to disable that Create Button and It will be enabled only if there are 3 checks on the dialog.

I am using a GridPane on Dialog and I was thinking about returning those certain nodes (Checks which are ImageViews) at those cells and check whether the condition is true. However, I could not figure out how to return a node from GridPane. If you have any other approach for this problem it is fine too.

This is the code's relevant part.

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


}

Solution

  • Create an appropriate BooleanBinding which expresses when the button should be disabled. You can use the Bindings utility class to create the expression, including comparison, ands and ors. To make the code more readable do a static import of the functions.

    Get the create button from your panel and bind the boolean expression to the disable property of your button.

    If any of the values change the JavaFX framework will automatically reevaluate the bindings and update the button's state accordingly.

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

    You can use the same mechanism to control the visibility of each checkmark. Create a 'incomplete' BooleanBinding for each textfield and bind it with a not binding to the visible property of the checkmark. Use all these BooleanBindings in a compound or to determine the button state. This way the button state and checkmarks will always be in sync.