简体   繁体   中英

Java Interface in InputVerifier

I have this code:

 public class Anagrafica implments ClientiInterface{    
 InputVerifier verifierAliquotaIva = new InputVerifier() {
    public boolean verify(JComponent input) {
        boolean verifica = true;
        final JTextComponent source = (JTextComponent) input;
        String text = source.getText();
        if (text.length() != 0){
            String codice = cliente.CercaCliente(text, this);
            if (codice != null){
                verifica = true;
            }else{
                JOptionPane.showMessageDialog(null, "Codice iva inesistente!");
             tfDescrizioneIva.setText("");
                verifica = false;
            }
        }else{
            tfDescrizioneIva.setText("");
        }
        return verifica;
    }
};

}

This is an Clientiinterface. I saw that interface is incompatible within InputVerifier . How can I resolve this problem?

If I understood correctly of what you are trying to achieve, you must use the following:
public class MyInputVerifier implements InputVerifier { ... }
instead of this:
InputVerifier verifierAliquotaIva = new InputVerifier() { ... }

and then use new MyInputVerifier() where needed.

More scientifically, an interface is only a skeleton, it has no implementation. If you want custom code in a place where InputVerifier is required, create a class that implements it, and use an instance of your new class

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