我正在創建一個應用程序,使用Javafx在其中我創建兩個不同的GridPanes說GridPane A和GridPane B.在GridPane A我有TextFields和GridPane B我有TextFields,Checkboxes和DatePickers,所有節點我在運行時創建。同時根據GridPanes A和B的我有一個按鈕,像這樣:JavaFx:如何同時在兩個GridPanes節點上應用驗證?
我想是直到兩個GridPane A和B. GridPane兩個條目我能上禁用按鈕實現它,但只有在GridPane一個這樣
首先通過檢查GridPane A的總的TextField被填充,而不是空:
private static boolean isAllFilled(GridPane tableA){
for(Node node : tableA.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;
}
然後驗證表A(GridPane A)和添加的偵聽:
private void validateTable(GridPane tableA, Button button) {
for(Node node : tableA.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(tableA)){
button.setDisable(false); // then make the button active again
}
else{
button.setDisable(true); // or else, make it disable until it achieves the required condition
}
});
}
}
}
我想在這兩個GridPane A和GridPane乙同時應用驗證,就像現在我的計劃是驗證GridPane的參賽作品和禁用/啓用按鈕,但我要的是保持,除非兩個GridPane A和GridPane禁用按鈕B條目已填寫。
從實用性的角度來看,你可能要考慮留在任何時候啓用按鈕,如果用戶按下它當任何文本字段爲空時,顯示[錯誤警報](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.AlertType.html#ERROR)信息性消息。用戶可能不明白爲什麼該按鈕被禁用或如何使其被啓用。 – VGR
你是對的,但它是項目的要求。 – Junaid