I post new question based on this previously code posted here (Filter users values on TextField input after a BindDirectional strategy betwen a Slider and min/max TextField)
My goal is simple, what is the best way to undo wrong TextField
value (based on custom verification) after user lost the focus event on my value.
Only way is to access the oldValue before user overwrite with another focus event ?
Actually i have this simple code :
val myTextField = new TextField ()
def parseDouble(s: String) = try {
Some(s.toDouble)
} catch {
case _ ⇒ None
}
myTextField.focusedProperty().addListener(
new ChangeListener[java.lang.Boolean]() {
override def changed(observable: ObservableValue[_ <: java.lang.Boolean], oldValue: java.lang.Boolean, newValue: java.lang.Boolean) {
if (!newValue) {
parseDouble(myTextField.textProperty().get()) match {
case Some(d: Double) ⇒ // test on the double value entered by user
case _ ⇒ // code to reset to old value ??
}
}
}
})
Update 1 :
I find discussion here : https://forums.oracle.com/forums/thread.jspa?threadID=2382472 about undo functionnality for TextField/TextArea and other source code about TextInputControlBehavior
: https://forums.oracle.com/forums/thread.jspa?threadID=2438759&tstart=45
I find description of undo behavior implemented into javafx 2.2 here http://javafx-jira.kenai.com/browse/RT-7547 but i cannot find sample code...
Update 2 :
I find a post for public undo control API (roadmap fixed for 2.2.6) for TextInputControl
here : http://javafx-jira.kenai.com/browse/RT-26575
TextInputBehaviorControl
can be see here : https://bitbucket.org/shemnon/openjfx-rt/src/6696e9cea59c401d2637d82f9cf96a515d210203/javafx-ui-controls/src/com/sun/javafx/scene/control/behavior/TextInputControlBehavior.java
Update 3 :
Tadam !
Finally i found an horrible way to do that, i hope public API is for 2.2.6 version of javaFX ...
val myTextField = new TextField ()
def parseDouble(s: String) = try {
Some(s.toDouble)
} catch {
case _ ⇒ None
}
myTextField.focusedProperty().addListener(
new ChangeListener[java.lang.Boolean]() { db ⇒
override def changed(observable: ObservableValue[_ <: java.lang.Boolean], oldValue: java.lang.Boolean, newValue: java.lang.Boolean) {
if (!newValue) {
parseDouble(myTextField.textProperty().get()) match {
case Some(d: Double) ⇒
if (myTextField.minValue > d || d > myTextField.maxValue) {
doubleField.getSkin.asInstanceOf[TextInputControlSkin[_, _]].getBehavior.asInstanceOf[TextInputControlBehavior[_]].callAction("Undo")
} else {
// Here you change value property of textField
}
case _ ⇒ myTextField.getSkin.asInstanceOf[TextInputControlSkin[_, _]].getBehavior.asInstanceOf[TextInputControlBehavior[_]].callAction("Undo")
}
}
}
})
I validate the answer if anybody find a better way to do that :)