Domanda

I have a button that is disabled at default. I want the button enabled when my textfield is no longer empty. I'm trying to use a documentlistener but it gives the following error:

method addDocumentListener in interface javax.swing.text.Document cannot be applied to given types;
  required: javax.swing.event.DocumentListener
  found: <anonymous DocumentListener>
  reason: actual argument <anonymous DocumentListener> cannot be converted to javax.swing.event.DocumentListener by method invocation conversion

Here is my code:

    private void tekstveldActionPerformed(java.awt.event.ActionEvent evt) {

    tekstveld.getDocument().addDocumentListener(new DocumentListener(){
        public void changedUpdate(DocumentEvent e) {
            changed();
        }
        public void removeUpdate(DocumentEvent e) {
            changed();
        }
        public void insertUpdate(DocumentEvent e) {
            changed();
        }

        public void changed(){
            if (tekstveld.getText().equals("")){
                leegmaken.setEnabled(false);
            }else{
                leegmaken.setEnabled(true);
            }
        }

    });        
}
È stato utile?

Soluzione

Have you (maybe erroneously) declared your own class DocumentListener somewhere else? i.e. what happens if you change

tekstveld.getDocument().addDocumentListener(new DocumentListener(){

to

tekstveld.getDocument().addDocumentListener(new javax.swing.event.DocumentListener(){

?

(Although, on an related note, I agree with the comment, above, that setting a document listener inside your action performed method seems like a bad idea; this should be set when your form is being created..)

Altri suggerimenti

reason: actual argument cannot be converted to javax.swing.event.DocumentListener by method invocation conversion.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top