gpt4 book ai didi

JavaFx:如何在运行时验证多个文本字段的创建?

转载 作者:行者123 更新时间:2023-11-30 02:31:06 24 4
gpt4 key购买 nike

我在运行时使用 for 循环创建多个 TextFields 并将它们添加到 Gridpane(有 8 列)内部,如下所示:

public static GridPane table(int rows){
GridPane table = new GridPane();

for(int i=0; i<rows; i++){
JFXTextField textField1 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField2 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField3 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField4 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField5 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField6 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField7 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
JFXTextField textField8 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);

//add them to the GridPane
table.add(textField1, 0, i+1);
table.add(textField2, 1, i+1);
table.add(textField3, 2, i+1);
table.add(textField4, 3, i+1);
table.add(textField5, 4, i+1);
table.add(textField6, 5, i+1);
table.add(textField7, 6, i+1);
table.add(textField8, 7, i+1);
}
return table;
}

接下来,我将创建另一种方法来从特定行和列的表中返回组件,如下所示:

public static Node getComponent (int row, int column, GridPane table) {
for (Node component : table.getChildren()) { // loop through every node in the table
if(GridPane.getRowIndex(component) == row &&
GridPane.getColumnIndex(component) == column) {
return component;
}
}

return null;
}

问题在这里:我想验证每个文本字段,因此如果用户忘记在任何文本字段中写入,我想禁用按钮,为此目的我使用绑定(bind)像这样:

 private void validatingGrid() {
GridPane table = (GridPane) anchorPane().getChildren().get(0);

for(int i=1 ; i<=comboBox().getValue(); i++){
JFXTextField text0 = ((JFXTextField)getComponent (i, 0, table));
JFXTextField text1 = ((JFXTextField)getComponent (i, 1, table));
JFXTextField text2 = ((JFXTextField)getComponent (i, 2, table));
JFXTextField text3 = ((JFXTextField)getComponent (i, 3, table));
JFXTextField text4 = ((JFXTextField)getComponent (i, 4, table));
JFXTextField text5 = ((JFXTextField)getComponent (i, 5, table));
JFXTextField text6 = ((JFXTextField)getComponent (i, 6, table));
JFXTextField text7 = ((JFXTextField)getComponent (i, 7, table));

button.disableProperty().bind(
Bindings.isEmpty(text0.textProperty())
.or(Bindings.isEmpty(text1.textProperty()))
.or(Bindings.isEmpty(text2.textProperty()))
.or(Bindings.isEmpty(text3.textProperty()))
.or(Bindings.isEmpty(text4.textProperty()))
.or(Bindings.isEmpty(text5.textProperty()))
.or(Bindings.isEmpty(text6.textProperty()))
.or(Bindings.isEmpty(text7.textProperty()))
);
}
}

但是发生的情况是它只验证最后一行,假设我在 Gridpane 中创建 3 行 textfeilds,所以它只验证第三行而不是第一行和第二行,并且根据第三行条目启用按钮,但是我希望在验证所有行后,它应该启用按钮,否则不会。请帮助我如何实现这一目标。

最佳答案

你的绑定(bind)逻辑是正确的。然而,问题是因为for loop [for(int i=1 ; i<=comboBox().getValue(); i++) ],这会毁掉你的工作。全部TextFields位于列索引 0唯一改变的是行索引。所以你应该使用getComponent(i, 0, table);对于所有人TextFields在你的for loop不将列索引更改为 1 , 2 .. 等等。但这也无法解决问题,因为在每个循环中您都分配 ALL TextFields到相同的索引,然后在每个循环中覆盖它,直到它们全部指向 TextField在索引comboBox().getValue()和列0 (这就是为什么它适用于您提到的最后一行)。

我建议采用不同的方法,如下所示:

首先,您需要一种方法来检查是否所有其他 TextFields已满/非空:

/**
* Check if all the TextFields are filled and not empty
* @param table
*/
private static boolean isAllFilled(GridPane table){
for(Node node : table.getChildren()){ // cycle through every component in the table (GridPane)
if(node instanceof TextField){ // if it's a TextField
// after removing the leading spaces, check if it's empty
if(((TextField)node).getText().trim().isEmpty()){
return false; // if so, return false
}
}
}
return true;
}

其次,聆听每个TextField的文本变化在表中,每次更改时,检查是否所有其他 TextField已满/非空:

/**
* To Validate the Table (GridPane)
* This method should be added to the tabPane change listener
* @param table
* @param button
*/
private void validateTable(GridPane table, Button button) {

for(Node node : table.getChildren()){ // cycle through every component in the table (GridPane)
if(node instanceof TextField){ // if it's a TextField
((TextField)node).textProperty().addListener((obs, old, newV)->{ // add a change listener to every TextField
// then check if the new value is not empty AND all other TextFields are not empty
if(!newV.trim().isEmpty()&&isAllFilled(table)){
button.setDisable(false); // then make the button active again
}
else{
button.setDisable(true); // or else, make it disable until it achieves the required condition
}
});
}
}

此外,您需要将按钮设置为在创建后禁用一次。

Button button = new Button("Test"); 
button.setDisable(true);

最后需要添加 tabPane 中的方法更改监听器 block :

tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
.........
.........
.........
validateTable((GridPane) anchorPane().getChildren().get(0), test);
}
<小时/>

测试

Testing

关于JavaFx:如何在运行时验证多个文本字段的创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44256865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com