3

I wrote my own InputVerifier for JTextField and it works fine. Now I would like to "force" validation on this text field when user clicks a button. Is it possible to do?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jarek Mazur
  • 2,052
  • 1
  • 25
  • 41
  • 1
    validations could be in JTextField, not from JButton, for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Jan 16 '13 at 19:39
  • +1 mKorbel and Mad comments...See [this](http://stackoverflow.com/questions/13563042/programmatically-trigger-a-key-events-in-a-jtextfield/13563314#13563314) similar question/answer. It shows use how to programmatically trigger `JTextField` *enter* key pressed using Robot class and `requestFocusInWindow()`, though I think all you will need is changing focus to the textfield and to another component via `requestFocusInWindow()`... – David Kroukamp Jan 16 '13 at 19:41
  • 2
    The Java docs state *"Before focus is transfered to another Swing component that requests it, the input verifier's shouldYieldFocus method is called. Focus is transfered only if that method returns true"* – MadProgrammer Jan 16 '13 at 19:41
  • 1
    Thank you for your answers. Changing focus back and forth triggers text field validation. Please write an answer so I can mark it :) – Jarek Mazur Jan 16 '13 at 19:51

1 Answers1

2

Added as answer as @JarekMazur said it solved the problem.

Re-iterating my comment:

+1 @MadProgrammer for the docs

See this similar question/answer. It shows use how to programmatically trigger JTextField enter key pressed using Robot class and requestFocusInWindow(), though I think you would only need the latter.

Try changing JTextField focus using requestFocusInWindow().

You would initially request focus on the JTextField and than to another component in order to fire the Input Verifier shouldYieldFocus method.

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • This looks like a workaround. If I have several controls with verifiers should I transfer focus to all of them sequentially? It would be nice if I could fire a method like `verifyAll`. Nothing like that in Swing? – Jarekczek Nov 23 '13 at 21:03