2

I am using a javafx textfield. I need to enter numbers along with decimals that should match the format ###.###.###.###
where the decimals should not be editable at all, and the numbers can be any digit; no other input is allowed. Not all digit placeholders need to be filled , e.g. it could be 1.2 or 2.22.1 or even 1.222.222.0

I have tried using Pattern matching with regex to validate the altered text, but that's all it does; it validates input, it does not make the decimal field a non editable field, and tooltip is not an option. I am not sure of what else to try.

I am trying to find a way so that even while users type they can see what the expected input is e.g. 1##.###.###.###. While they are entering numbers, they do not enter the decimal as it is 'fixed' in the text field, or atleast appears to be. If continuous number input is received, the typed numbers skips the decimal when it encounters it and automatically goes to the next field. e.g. user input ="1234" output="123.4##.###.###"

another suggestion would be to automatically advance the cursor when a decimal is received e.g. user input = "1.23.4" output = "001.023.004.###"

Any and all suggestions are welcome, and just for clarification the "#" is part of the textfield 'hint' and not part of the actual text.

user2570
  • 31
  • 3
  • 1
    Sounds like you need a textformatter. – SedJ601 Jul 05 '19 at 21:28
  • I tried that as well, but the decimal is still editable. – user2570 Jul 08 '19 at 13:56
  • Textformatter is what you need. How to implement it is a different story. I think you can have a regex of all the possible input states and only allow those states. Someone more versed in TextFormatter and Regex could help you more probably. – SedJ601 Jul 08 '19 at 14:28
  • 1
    I ended up doing exactly as you suggeted, using regex to control input for change and using string converter to process the string when focus was lost. tyvm! I have solved it for now. – user2570 Jul 10 '19 at 18:12
  • @Sedrick no, never-ever use manual pattern matching in (potentially) Locale-sensitive conversions. Instead use the appropriate Format. – kleopatra Aug 04 '19 at 10:36
  • @kleopatra even though I made that suggestion, I tried to make it clear that this was beyond me and that the user should wait for an answer from someone with knowledge about the topic. – SedJ601 Aug 05 '19 at 05:48

1 Answers1

1

In the end, used UnaryOperator with regex for input control and StringCoversion to post process text as used here How to force a double input in a TextField in JavaFX? in case anyone else runs into this problem.

user2570
  • 31
  • 3
  • that's a very outdated approach with limited success (it will not survive a Locale change, f.i.) - you want to use a TextFormatter as you were already advised to use in a comment to your question. If you have problems doing so, you could have posted another question ;) – kleopatra Aug 04 '19 at 10:33