簡體   English   中英

從GridPane上的單元返回節點。 (JavaFX)

[英]Returning a node from a cell on GridPane. (JavaFX)

我有一個學校項目或類似的項目,並且正在嘗試為用戶創建一個注冊面板。 當用戶單擊“注冊”時,此面板打開。 看起來像這樣。

注冊對話框

我想做的是我想禁用該“創建”按鈕,並且僅當對話框上有3個檢查時才會啟用。

我在Dialog上使用了GridPane,正在考慮返回那些單元格處的某些節點(檢查為ImageViews),並檢查條件是否為真。 但是,我不知道如何從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();


}

我不知道如何從GridPane返回節點。

    gridPane.getChildren() 

提供了節點列表,但是您已經有了組件textFieldDialogUserIDpasswordFieldDialogPasswordpasswordFieldDialogConfirmPassword

=>為它們每個添加一個動作偵聽器,該偵聽器會在更改其值時檢查這些值。 根據結果​​,啟用/禁用“創建”按鈕(默認情況下,應將其禁用)。

您可以舉一個例子: http : //docs.oracle.com/javafx/2/ui_controls/text-field.htm

創建一個適當的BooleanBinding ,它表示何時應禁用按鈕。 您可以使用Bindings實用程序類創建表達式,包括比較和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);

您可以使用相同的機制來控制每個選中標記的可見性。 為每個文本字段創建一個“不完整”的BooleanBinding ,並將其綁定not綁定到選中標記的visible屬性。 在化合物中使用所有這些BooleanBindings or確定按鈕狀態。 這樣,按鈕狀態和復選標記將始終保持同步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM