2

I'm trying to do the following. I have a TextField (or any other control) and I want to determine focus loss according to user’s input validation. I’ve read this article https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification but it seems that JavaFX does not handle focus as Swing does.

What I’m trying to achieve is: “A component's input verifier is consulted whenever the component is about to lose the focus. If the component's value is not acceptable, the input verifier can take appropriate action, such as refusing to yield the focus on the component or replacing the user's input with the last valid value and then allowing the focus to transfer to the next component.”

When a user is focused on a textfield (or any other control) I want to validate user’s input in 3 scenarios: 1) Enter key was pressed (I would listen to the KeyEvent, validate input and, if appropriate, ask to focus on the next control, but I don’t know how to do the latter). 2) TAB key was pressed (I need to intercept the focus change event). 3)Focus is lost (for example by clicking on another control or outside the Stage or even by pressing TAB key)

I need to validate user’s input and decide whether I let focus loss or no. In a way, I need to intercept the focus change event.

I can’t simply listen to de textField.focusedProperty because that only tells me that I’m loosing focus, but I can’t (or at least I don´t know how) stop it from happening.

I tried to get information about focus subsystem in JavaFX but couldn’t find any.

I’d like to know when the engine handles focus events and act according to: a) The control that is loosing focus (and its content) b) The possible next control in the focus sequence. c) If the focus remains in the same Stage o if its send to another Stage or application.

I hope I’ve been clear enough with my explanation and please forgive my English if there are any mistakes.

Thank you very much in advance.

Damian
  • 31
  • 4
  • What is wrong with calling `textField.requestFocus` if you want to re-focus the text field after focus was lost? – Itai Mar 02 '16 at 15:18
  • 1
    @sillyfly Bad things will happen if you have two text fields set up like that, and move focus from one to the other when both have invalid text (which is a perfectly reasonable scenario...). – James_D Mar 02 '16 at 15:30
  • Hmm... not really - how would an invalid text get to the second text-field? But I'd love to hear a better solution! – Itai Mar 02 '16 at 15:52
  • If you assume they all start in a valid state then it can't. But this is not necessarily true in a form: often you only want to validate after the user has accessed the control (e.g. sometimes there is no sensible default for a required value). Though I do not know of a better solution. I think there's a general RFE for focus management API... – James_D Mar 02 '16 at 16:42
  • Thanks for the anwsers. @sillyfly, The problem with that approach is that if I let focus to be lost on the first control and gained in the second, when I call requestFocus on the first, it will fire the same event on the second control, trying to validate it, which will lead to an error due to the lack of user input. – Damian Mar 02 '16 at 18:15
  • @James_D, it's true, not all of the controls are initially in a valid state. For example, some of them can´t be empty nor have a default value. – Damian Mar 02 '16 at 18:26
  • Continuing with my last comment, in this case the user would experience the following: 1) He is editing one Textfield and presses TAB. 2) My code would execute the FocusPropertyChangeListener where I validate user Input. 3) That input results invalid and a dialog is shown. 4) Focus passes to the second TextField anyway since I can´t prevent this from happening. 5) Then RequestFocus on the first control is called 6) Which fires focus lost on the second. 7) Which fires input validation. – Damian Mar 02 '16 at 18:28
  • In that case, since the user didn´t get the chance to type anything, we have an invalid input again and another dialog show which, in my opinion, is not friendly for the user experience. – Damian Mar 02 '16 at 18:28
  • I think the best you can do here is essentially as @sillyfly suggests. You can work around the problem of multiple controls not being in a valid state by setting a flag that suppresses the validation. It's a bit ugly, and I generally dislike the idea of implementing anything by changing something back when it gets changed to something you don't want it to be (at a minimum, it would produce difficult to interpret results in other focus listeners), but with the current API it may be the best you can do. – James_D Mar 02 '16 at 19:35
  • @Damian hmm .. can't reproduce such loops - ohh wait: it's the dialog that's interfering? Anyway, please show a SSCCE that demonstrates the problem - you know the drill, don't you :-) – kleopatra Mar 03 '16 at 16:08

0 Answers0