我在运行时使用for循环创建多个TextField,并将它们添加到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;
}
问题在于:我想验证每个TextField,所以如果用户忘记在任何TextField中写入,我想禁用Button,为此我正在使用绑定像这样:
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行textfeild,所以它只验证第3行而不是第1行和第2行,并且根据第3行条目启用按钮但是我想验证它应该启用按钮后的所有行,否则不行。请帮助我如何实现这一目标。
答案 0 :(得分:2)
您的绑定逻辑是正确的。但是,问题是因为for loop
[for(int i=1 ; i<=comboBox().getValue(); i++)
],这会破坏您的工作。所有TextFields
都位于列索引 0
,唯一更改的是行索引。因此,对getComponent(i, 0, table);
中的所有TextFields
使用for loop
而不将列索引更改为1
,2
..并且等等。但是,这也无法解决问题,因为在每个循环中,您都要将 所有 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
更改侦听器阻止中添加该方法:
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
.........
.........
.........
validateTable((GridPane) anchorPane().getChildren().get(0), test);
}
<强>测试强>