我有一些 TextFields 在失去焦点时执行操作。我在他们的focussedProperty 上附加了一个ChangeListener,一切正常。但是当我使用 System.exit(0) 离开应用程序时,没有执行最终更改。这是我的听众:
public abstract class TextUpdateListener implements ChangeListener<Boolean> {
private TextField field ;
private String initialText ;
private StringProperty text ;
protected TextUpdateListener(TextField field){
this.field = field ;
initialText = "";
text = field.textProperty();
}
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
Boolean newValue) {
if(newValue == true){
System.out.println("focus gained");
initialText = field.getText().trim();
System.out.println("Text on focus gained : "+text.get());
System.out.println("InitialText on focus gained : "+initialText);
}
if(newValue == false){
System.out.println("Text on focus lost : "+text.get());
if(initialText.isEmpty()){
if( ! text.get().isEmpty()){
onCreated();
}
}
if(! initialText.isEmpty()){
if(text.get().isEmpty()){
onDeleted();
}
else if(! text.get().equals(initialText)){
onChanged();
}
}
}
}
public String getInitialText(){
return initialText ;
}
public TextField getField(){
return field ;
}
public abstract void onDeleted();
public abstract void onChanged();
public abstract void onCreated();
}
我真的很感激一些帮助!