Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8101931

Add method to filter text added to a TextField



    • Enhancement
    • Status: Closed
    • P4
    • Resolution: Not an Issue
    • fx2.1
    • None
    • javafx
    • None


      In attempting to write a number of text-based controls for samples or tutorials, I'm always blocked by the fact the there is no good way to filter or restrict the input of a TextField. Although we have a FormattedTextField slated for 2.2 (which, by the way, I do not think we should do as a separate control but actually just add the format / mask to TextField or TextInputControl), I may want to do some very custom text input customization that a Format / mask just wouldn't work for (maybe it is context sensitive).

      At first I wished that TextField had a protected constructor taking a Content -- that way I could just subclass TextField and produce my own Content restricting the text as I saw fit. However this also means exposing the TextField's TextFieldContent as something that can be overridden, which I'd prefer not to do.

      My next thought was to add a filterInput(int index, String text) method which would be called by TextFieldContent.insert. However this doesn't help restrict deletions which also needs to be done.

      So maybe we can just add an "accept" method which takes the new proposed string (whether the result of adding text or removing text) and then it returns true or false.

      The problem is I want to avoid letting somebody type the incorrect key and the string of the text field being incorrect for a moment and then I change it back to something valid. It would be nicer to restrict the input so it can't go wrong in the first place.


        Issue Links



              jgiles Jonathan Giles
              rbair Richard Bair (Inactive)
              0 Vote for this issue
              1 Start watching this issue