Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:11:36+00:00 2026-06-18T12:11:36+00:00

I post new question based on this previously code posted here ( Filter users

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:11:37+00:00Added an answer on June 18, 2026 at 12:11 pm

    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!

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

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

    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) { ... }
    )
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously posted here: Controlling Access for Trial Subscription Since this is a new
This question is in continuation to my previous post located here . Since there
I have almost the same question as this . Based on that post I
I have been asked to post a new question about how to correctly sort
Here is an example of what causes the error: ruby-1.9.2-p290 :004 > Post.new(title: new).save!
I'm developing a question/answer based application I have a Post table in the db
I posted a question about this earlier, but I have more information now and
Forgeive me if this is a dumb question, im very new to SL, just
This is based on generating a second x-axis, as described in this previous post:
I Posted the background to this question a few days ago.. but the answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.