Hi,
I'm writing a program with 2 JTextAreas. When the text in 1 area changes
the text in the other needs to be updated as well. I'm using
DocumentListener to handle this. However when the text is updated the
DocumentEvent is fired as well. The only solution I can think of is
removing the DocumentListener before updating the text and adding it
after that, but that would be very inefficient. Please give me some
recommendations. Thank you very much.
Chinh
Thomas Hawtin - 16 May 2006 15:20 GMT
> I'm writing a program with 2 JTextAreas. When the text in 1 area changes
> the text in the other needs to be updated as well. I'm using
[quoted text clipped - 3 lines]
> after that, but that would be very inefficient. Please give me some
> recommendations. Thank you very much.
It's highly unlikely that adding or removing a listener will account for
a significant time for each key stroke. However, it's probably easier to
use a flag.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Trung Chinh Nguyen - 16 May 2006 22:26 GMT
> It's highly unlikely that adding or removing a listener will account for
> a significant time for each key stroke. However, it's probably easier to
> use a flag.
You're right. I was abusing Listeners way too much ><
Roedy Green - 21 May 2006 21:42 GMT
On Tue, 16 May 2006 14:39:24 GMT, Trung Chinh Nguyen
<chinhnt2k3@gmail.com> wrote, quoted or indirectly quoted someone who
said :
> The only solution I can think of is
>removing the DocumentListener before updating the text and adding it
>after that, but that would be very inefficient. Please give me some
>recommendations. Thank you very much.
I would leave the connection in place. Just use a boolean to tell
the handler when to do its thing. You can flip that back and forth
with much less fuss than you can add and remove a listener. It
probably leaves far fewer dead objects lying in its wake too, but
don't quote me on that. I would think the code would be simpler and
easier to follow.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.