简体   繁体   中英

How does setInputVerifier(InputVerifier inputVerifier) work?

I've got code:

public class SerialNumberInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText();
    if(text.length()!=6)
        return false;
    try {
        Integer value = Integer.parseInt(text);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

It seems to work fine, do all the checkings. But I'm not sure about setInputVerifier(InputVerifier inputVerifier). It just doesn't allow me to input to another field, but I simply can push a button and a wrong data have been sended. So how could I possibly handle that?

Thanks.

Take a look at Validating Input from How to Use the Focus Subsystem .

Basically, what it is allowing you to do is determine if focus should be taken away from the field or not depending on whether you think the input is valid or not

Try overriding shouldYieldFocus to stop the field from losing focus when it's invalid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM