失去焦点后基于值验证对textField值的撤消行为 [英] Undo behavior on textField value based on value verification after focus lost

查看:154
本文介绍了失去焦点后基于值验证对textField值的撤消行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据此处发布的先前代码发布新问题(

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)

我的目标很简单,在用户将焦点事件丢失到我的值上之后,撤销错误的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.

唯一的方法是在用户用另一个焦点事件覆盖之前访问oldValue吗?

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 ??
        }
      }
    }
  })

更新1:

我在这里找到讨论: https://forums.oracle.com/关于撤消TextField/TextArea的功能和关于TextInputControlBehavior的其他源代码的forums/thread.jspa?threadID = 2382472 TextInputControlBehavior的其他源代码:

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

我在这里找到了实现到Javafx 2.2中的撤消行为的描述 http://javafx- jira.kenai.com/browse/RT-7547 但我找不到示例代码...

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...

更新2:

我在以下位置找到了TextInputControl的公共撤消控制API(路线图固定为2.2.6)的帖子: http://javafx-jira.kenai.com/browse/RT-26575

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可以在这里查看: 更新3:

塔当姆!

最后,我发现了一种可怕的方法,我希望公共API适用于javaFX的2.2.6版本...

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 :)

推荐答案

由于我目前也在使用Scala解决方案使用JavaFX,我想尝试一下您的示例,但无法对其进行编译.具体来说,doubleFieldvalue.set是未知的!

as I am currently working on a JavaFX with Scala solution too, I would like to try your example, but I can't get it compiled. Specifically doubleField and value.set are unknown!

我有一些提示- 首先:有一个明显的代码重复,可以通过在条件上添加一个条件来轻松解决-第一种情况仅在满足条件(范围内的d)时成立

I have some hints - First: there is an obvious code duplication, that could be easily solved by adding a condition to the case - the first case only holds if the condition (d in range) is fulfilled

case Some(d: Double) if (doubleField.minValue <= d && d <= doubleField.maxValue) ⇒
  value.set(d)
case _ ⇒ 
  doubleField.getSkin.asInstanceOf[TextInputControlSkin[_, _]].getBehavior.asInstanceOf[TextInputControlBehavior[_]].callAction("Undo")

第二个:为Java中的匿名内部类提供包装器-例如,上面的ChangeListener可以这样包装:

Second: provide a wrapper for anonymous inner classes in Java - for instance the above ChangeListener could be wrapped like this:

implicit def unit2ChangeListener[T](f: (ObservableValue[_ <: T], T, T) => Unit) =
  new ChangeListener[T] {
    override def changed(observable: ObservableValue[_ <: T], oldValue: T, newValue: T) {
      f(observable, oldValue, newValue)
    }
}

这些隐式转换可以隐藏在util类(以及一个不错的unit2EventHandler)中,并导入到您的应用程序中. 这将使内容更具可读性(但仍然有些痛苦):

These implicit conversions could be hidden in a util class (along with a nice unit2EventHandler) and imported to your application. This would lead to something a bit more readable (but still a little painful):

myTextField.focusedProperty().addListener(
  (observable: ObservableValue[_ <: java.lang.Boolean], 
     oldValue: java.lang.Boolean, newValue: java.lang.Boolean) =>
     if (!newValue) { ... }
)

ScalaFx 可能已经提供了类似的功能,但是我还没有尝试过.

Probably ScalaFx already provides something like this, but I haven't tried it yet.

这篇关于失去焦点后基于值验证对textField值的撤消行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆