Pergunta

I have a class that implements a DocumentListener:

    public class MyListener implements DocumentListener{

            private JTextField textField;

            public MyListener(JTextField textField) {
                LOGGER.info("listener constructor");
                this.textField = textField;
            }
            @Override
            public void insertUpdate(DocumentEvent e) {
                LOGGER.info("insert update");
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                LOGGER.info("removeupdate");
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
                LOGGER.info("CHANGED UPDATE");
            }   
}

I try to add the DocumentListener to my text field but when I run the program none of the logging statements are shown:

private JTextField createTextField() {

    JTextField tf = new JTextField(30);
    tf.setName("name");

    tf.getDocument().addDocumentListener(new MyListener(tf));

    return tf;
}

My Panel is a final so I don't know if that has something to do with it. And there are multiple panels on the gui.

What I'm basically trying to do is to enable the Save button when the user enters a name in the JTextField and when they erase the name, I will disable the save button. \

Thoughts?

Foi útil?

Solução

If the

LOGGER.info("listener constructor"); 

isn't shown then that suggests to me your logging is not set up correctly to output info-level messages. I tried your code with System.out.println() in place of LOGGER.info and it worked as expected.

Outras dicas

Given your code, there's no reason why this shouldnt work. More than likely the trace level of your logger is set to a higher level (e.g. ERROR) preventing any output from appearing. Ensure the trace level is at least set to INFO.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top