我正在開發使用JavaFX中,我創建一個GridPane內動態文本字段和有它最初像這樣禁用的按鈕的應用程序:JavaFx:如何比較GridPane中動態創建的TextField的值?
所以,我要的是,如果第1列TextField的值小於第3個的TextField值,按鈕應使這樣的:
但我們說,如果任何列3文本字段值變得小於同一行的第1個文本字段的值,它應該disab樂按鈕,並以紅色顯示的具體文本字段邊緣,當鼠標懸停在該字段應該顯示一些警告:
我創建文本字段是這樣的:
public static GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField1 = new JFXTextField();
textField1.setAlignment(Pos.CENTER);
TextField textField2 = new JFXTextField();
textField2.setAlignment(Pos.CENTER);
TextField textField3 = new JFXTextField();
textField3.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);
}
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;
}
我試圖做這樣的b UT它不工作(在這裏,我將值轉換成字符串和公正的檢查比較):
private boolean isTextEqual(GridPane table, Button button){
for(Node node : table.getChildren()){
if(node instanceof TextField){
for(int i=1 ; i<=ComboBox().getValue(); i++){
String str = ((TextField)DynamicGridpanes.getComponent (i, 0, table)).getText();
((TextField)DynamicGridpanes.getComponent (i, 2, table)).textProperty().addListener((obs, old, newV)->{
if(newV.toString()==str){
button.setDisable(false);
}
else{
button.setDisable(true);
}
});
}
}
}
return true;
}
你仍然有'DatePicker'和'CheckBox'在你的餐桌? – Yahya
是@Yahya我有。我發佈它就像簡化問題 – Junaid
所以基本上你有三列'TextFields'(後面是對方),他們是'CheckBoxs',然後是'DatePickers'?並且是每個'TextField'列在不同'GridPane'中..你有多少個'GridPane'? – Yahya