1

I have a swing application with JTextFields that I attach InputVerifiers to. I have valid defaults applied to the fields via the setText method, e.g.

this.myField.setText("11");

But it seems that public boolean verify(JComponent component) doesn't get called unless focus is applied to the fields. I've tried to programatically request focus, but that still doesn't seem to fire the InputVerifier, e.g.

this.myField.requestFocus();

How can I progrmatically set textfield text and get my InputVerifier to fire and run its verify() method?

I could manually fire the InputVerifier after I construct it, calling verify() and passing in the component, but that seems really unnecessary since the TextField is already wired up to the InputVerifier.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • InputVerifier by default firing those event if is there implemented Swing Action(ev ActionListener), by containing shouldYieldFocus() & verify, could be applied for JTextComponents and derived editors for JSpinner and JComboBox, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable – mKorbel Apr 30 '13 at 12:43
  • [your SSCCE could be based on](http://www.java2s.com/Code/Java/Swing-JFC/InputVerificationDemo.htm) – mKorbel Apr 30 '13 at 12:45
  • I think you're saying to attach an Action that will fire and call the input verifier methods? When would this action be fired though? – Stealth Rabbi Apr 30 '13 at 12:46

2 Answers2

3

I have valid defaults applied to the fields via the setText method

If the default is valid why do you need to invoke the input verifier?

I've tried to programatically request focus

The proper method to use is:

component.requestFocusInWindow()

however, this only works when requesting focus on a component on a visible GUI.

where is there something that lets you check the validity status of all your fields

This logic should be part of the "Submit" button. The InputVerifier should only check for data validity for the field. The "Submit" logic checks to make sure data for all fields has been entered.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • So, the logic for validity is in the InputVerifier, but how would the submit button behavior check the validity of all of the fields, besides calling verify(component) explicitly ? – Stealth Rabbi Apr 30 '13 at 16:11
  • The Submit button doesn't need to validate each component, the InputVerifier already did that. All the Submit button needs to do is make sure that all text fields contain data (ie. the text field is not empty). – camickr Apr 30 '13 at 16:33
  • If your InputVerifier doesn't keep focus though, you can have invalid fields that need to be checked. That's where my issue is. – Stealth Rabbi Apr 30 '13 at 16:42
  • No you can't have invalid fields just because the component loses focus. The input verifier is invoked when the text field loses focus. So you validate the text field at that time. The only way the data can change is if the text field gains focus again. So when it loses focus it will be verified again. – camickr Apr 30 '13 at 23:49
  • In my case, shouldYieldFocus() always returns true. The field is validated, but gives the focus up. I don't want the behavior of forcing the user to be "stuck in the field". Yes, if the default is valid, it shouldn't have to be invoked, but I right now, it has a flag that determines if the field was 'valid' after the last verify() call, which isn't being called. – Stealth Rabbi May 01 '13 at 11:52
1

You can set a DocumentFilter on myField. Your verify() shouldn't have any side effects, so you can use it in the filter. The filter will get applied when you call setText(), or anytime the Document is changed.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • My Input Verifiers do have side effects though. maybe I'm doing it wrong. They set an "is valid" flag that can later be queried to determine if all of the fields on the form are valid. They also decorate the field based on if it's passing validation or not. – Stealth Rabbi Apr 30 '13 at 13:03
  • The [API](http://docs.oracle.com/javase/7/docs/api/javax/swing/InputVerifier.html) says, "This method should have no side effects." – Catalina Island Apr 30 '13 at 13:09
  • Sorry this is a bit beyond my original question, but it's not clear how you would have InputVerifiers govern the ability to do a 'submit' function on a form. You can indepedently check each field, but if the field gives up focus, where is there something that lets you check the validity status of all your fields to determine if you can submit the form? This is why I created my own class to manage this, and I just need to manually fire the verifier on request. Maybe the DocumentFilter is the answer. – Stealth Rabbi Apr 30 '13 at 13:25
  • If you don't want the user to be "stuck in the field," you can enable your `Submit` button only when your fields are all valid. You can do the check in each field's `shouldYieldFocus()` and `actionPerformed()`. A status box is helpful, too. – Catalina Island May 01 '13 at 13:23