我有一个学校项目或类似的东西,我正在尝试为用户制作一个注册面板。当用户点击注册时,此面板将打开。它看起来像这样。
我想要做的是我想要禁用该创建按钮,只有在对话框上有3个检查时才会启用它。
我在Dialog上使用GridPane,我正在考虑在这些单元格中返回那些特定节点(CheckViews是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();
}
答案 0 :(得分:1)
我无法弄清楚如何从GridPane返回一个节点。
gridPane.getChildren()
提供了节点列表,但您已经拥有了textFieldDialogUserID
,passwordFieldDialogPassword
,passwordFieldDialogConfirmPassword
组件。
=&GT;为每个动作侦听器添加一个动作侦听器,在其值发生更改时检查值。根据结果,启用/禁用“创建”按钮(默认情况下,应禁用它)。
你可以举个例子:http://docs.oracle.com/javafx/2/ui_controls/text-field.htm
答案 1 :(得分:1)
创建一个适当的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);
您可以使用相同的机制来控制每个复选标记的可见性。创建一个不完整的&#39;每个文本字段BooleanBinding
,并将其与not
绑定绑定到复选标记的visible
属性。在复合or
中使用所有这些BooleanBinding来确定按钮状态。这样,按钮状态和复选标记将始终保持同步。