• Post Reply Bookmark Topic Watch Topic
  • New Topic

string change event  RSS feed

 
Ranch Hand
Posts: 208
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it posible to detec that string or stringbuffer or ... has changed and perform then some action on it ?


what I mean:

instead of:



then there would be


and linebreak would be, on each line, added automaticaly.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miran Cvenkel wrote:is it posible to detec that string or stringbuffer or ... has changed and perform then some action on it ?


First - realize that a String is immutable - so it can't change and so would not have any mechanism for knowing when it changed. StringBuffer is a different story, it can change, but there isn't a way to automatically detect it the way you want. What I would suggest would be to use a 'wrapper' class or a extension of StringBuffer which you use instead of StringBuffer to do what you want. Maybe override the 'append()' method(s) and add the line break at the end of any new text.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:extension of StringBuffer

Except StringBuffer and StringBuilder are final so you can't extend them. Wrapping is the only way.
 
Miran Cvenkel
Ranch Hand
Posts: 208
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this then:

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the idea. But I wouldn't make the member variable public, that's a bad practice from an object oriented design point of view. And use StringBuilder instead of StringBuffer, because it's more efficient (it doesn't have unnecessary synchronization).

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Don't forget to add a toString() method that returns b.toString().

Edit: Jesper beat me to it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!