why so many document listener events?

W

Wizumwalt

MyDocumentListener implements the DocumentListener interface. The
problem I'm having with it is that when I start my GUI, the
insertUpdate() and removeUpdate() methods that I overrode are
constantly being called even though I don't type anything in the text
field that the listener is attached to. It's kinda causing havoc in
MyDocumentListener where I handle the input.

Code:
MyDocumentListener listener = new MyDocumentListener(...);
myTextField.getDocument().addDocumentListener(listener);

// ... interface methods in MyDocumentListener

public void insertUpdate(DocumentEvent de) {
// ...
}

public void changedUpdate(DocumentEvent de) {
// ...
}

public void removeUpdate(DocumentEvent de) {
// ...
}

Supposedly these methods give notification that an insert or other
change happened in the document, but I didn't make any changes, yet
they're constantly being called.

Anyone know why this is or understand what's going on here. Any help
much appreciated.
 
T

Trung Chinh Nguyen

There's nothing wrong with the code. The problem might lie somewhere
else. It would be clearer if you can provide the contents of the update
methods.
 
W

Wizumwalt

Thanks, while trying to explain it in a bit more detail (and a good
nights sleep :). I found what it was. Thanks again.
 
A

alexandre_paterson

Supposedly these methods give notification that an insert or other
change happened in the document, but I didn't make any changes, yet
they're constantly being called.

I don't have a precise answer to your problem but depending
on the API you're using and how you're using the Observer
pattern, you may more or less "legally" enter in an
non-stopping "event cascade" mode where many unnecessary
events are launched.

This is a well known shortcoming of the Observer pattern and
there exist papers on the subject. There are, of course,
workarounds.

One such paper can be found here:

http://www.gruntz.ch/papers/Observer.pdf

FWIW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top