Created
February 28, 2013 23:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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