Skip to content

Instantly share code, notes, and snippets.

@prule
Created February 28, 2013 23:46
Show Gist options
  • Save prule/5061164 to your computer and use it in GitHub Desktop.
Save prule/5061164 to your computer and use it in GitHub Desktop.
Griffon/JavaFX - Adding a change listener to a text field Part 3 - reusing the closure across multiple text fields
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import org.apache.commons.lang.StringUtils
// model
@FXBindable String amount1
@FXBindable String amount2
// view
textField(id: 'amount1', text: bind(model.amount1Property))
textField(id: 'amount2', text: bind(model.amount2Property))
noparent {
amount1.textProperty().addListener(controller.restrictToNumber)
amount2.textProperty().addListener(controller.restrictToNumber)
}
// controller
def restrictToNumber = { ObservableValue<? extends String> observable, String oldValue, String newValue ->
try {
if (StringUtils.isNotBlank(newValue)) {
Integer.parseInt(newValue);
}
} catch (Exception e) {
observable.setValue(oldValue);
}
} as ChangeListener<String>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment