Problem with DocumentListener

I posted this to the nbuser mailing list, but this is probably a better place to get a response.

I am a complete newbie to Java (after many years with Visual C++), and am writing my first project. I have hit a brick wall in NetBeans 5.5 trying to implement a DocumentListener. I noticed that even though I imported java.awt.* I also had to import java.awt.event.* to implement EventListener. So I have imported javax.swing.event.*, but NetBeans still cannot find DocumentListener. I get a "cannot find symbol" error, and the location is given as class javax.swing.JTextField.

These are the relevant bits of code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.event.*;

-

TopLevelListener tldl =new TopLevelListener();

-

topLevel =new JTextField(32);

topLevel.setMaximumSize(new Dimension(300, 25));

*topLevel.addDocumentListener(tldl);

--

*privateclass TopLevelListenerimplements DocumentListener

{

publicvoid insertUpdate(DocumentEvent e)

{

<some code>

}

}

I get error flags on the lines prefixed with *

Thanks for any help.

[1770 byte] By [JeffG1a] at [2007-11-27 2:01:40]
# 1

What package is DocumentListener in? I don't remember right now; I could look it up but you could do that too. My bet is that you haven't imported its package, whatever it is.

Important lesson for newbies: There's lots of documentation available. Use it.

http://java.sun.com/j2se/1.5.0/docs/

DrClapa at 2007-7-12 1:42:01 > top of Java-index,Java Essentials,New To Java...
# 2
You add the listener to the JTextField's document, not to the JTextField itself. Also, when you implement the DocumentListener interface, you have to provide implementations of all three of its methods.
uncle_alicea at 2007-7-12 1:42:01 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks - yes I know about documentation and have trawled through it. I am a Java newbie, not a programming newbie :)

Implementing the other two (unused) methods in the listener implementation fixed one problem (it remained abstract until I did that).

The problem I still have is with trying to call addDocumentListener from my JTextField object. That method is not listed in the selection popup (though addActionListener is).

JeffG1a at 2007-7-12 1:42:01 > top of Java-index,Java Essentials,New To Java...
# 4

> You add the listener to the JTextField's document,

> not to the JTextField itself.

Thanks - that should fix my other problem.

>Also, when you

> implement the DocumentListener interface, you have to

> provide implementations of all three of its methods.

Yep - I discovered that myself, thanks. I was posting when you posted that.

JeffG1a at 2007-7-12 1:42:01 > top of Java-index,Java Essentials,New To Java...