aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes TextArea and events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TextArea and events" Watch "TextArea and events" New topic
Author

TextArea and events

Julien Castelain
Greenhorn

Joined: Sep 27, 2005
Posts: 28
hello all,

I'm looking at the docs, and for the first time
i'm not using a JButton, a JList ,a JCombo or a mouse
with an event listener, but how do i add a listener for
a JTextArea instance, and what events are "broadcasted" ?

do i have to use the document model ?
and if by the way there was a good article on this
i'd be happy to know

thanks for your help
Julien Castelain
Greenhorn

Joined: Sep 27, 2005
Posts: 28
hi again so is this a good way to do it ?


another question though, is it better to create an inner class that implements CaretListener ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
I had a long discussion on the intermediate forum about this. Look on this thread.
It is really a case of,
  • how many components do you want to add the listener to, and
  • Are you using the same control (or similar) once, or several times?

  • If "one" and "once," then biting the bullet of the anonymous class, which you seem to be using, is probably best.

    I have never used a CaretListener, so I shall presume that your example works and does what you want it to.
    CR
    Julien Castelain
    Greenhorn

    Joined: Sep 27, 2005
    Posts: 28
    Thanks for the link CR


    I have never used a CaretListener, so I shall presume that your example works and does what you want it to.


    Well this "seems" to be working.
    All i want to see, is that when the text
    in the JTextArea instance changes, the new text is printed to the console
    (sorry for my bad english)...

    Since i'm using two jtextarea instances, i think i'll write an inner class
    but in general terms what are the pros and cons of inner classes vs anonymous classes ? except that you don't have to write you "event listening" code several times

    Anyway thanks a lot for the reply
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    If you want to be notified when the text in a JTextArea changes,
    I would use a DocumentListener. The caret is not the best thing
    to listener to: it may move when text is not added!


    There is no emoticon for what I am feeling!
     
     
    subject: TextArea and events