Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TextArea and events

 
Julien Castelain
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 29
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1780
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic