TextArea replace text

I have a textarea

textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
textArea.insert(myModel.commentToString(), 0);

Open in new window


When the dialog is created I have a display of what is in myModel.commentToString(), so this works great.  The problem is when the user closes the dialog and reopens the dialog, it seems to append the same text again.

Example

open dialog

in the textarea = Hello

close dialog
open dialog

in the textarea = Hello
in the textarea = Hello

Is there a way to just replace the text in the textarea?
jkteaterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
You need to clear what was in your model when you close the dilaog
0
CEHJCommented:
If the component has not been changed between closing and reopening, it won't change its text either
0
for_yanCommented:

Mkae sure that in the same method which closed the dialo you clear the
contents of myModel, so that this method retuns empty string:
 myModel.commentToString(),
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

for_yanCommented:
Is your textArae in your dialog which was cloased and then again opened - correct ?
0
jkteaterAuthor Commented:
yes - but they are not using the cancel or sumbit button to close it - they are clicking the X in the top right corner
0
for_yanCommented:
with the X in the top - you need to catch that even and
do the same you are douingwhen cloasing
0
for_yanCommented:
to do that you need to implement WindowListener interface and in the method
windowClosing() you need t do everything you are doing when they are clicking cancel
0
CEHJCommented:
>>yes - but they are not using the cancel or sumbit button to close it - they are clicking the X in the top right corner

It doesn't matter. If you're not changing the dialog's state, other than visibility then none of its child components will change either. You need to reset them if that's what you want to do
0
for_yanCommented:
public class MyDialog extends JDailog implements WindowListener {

...
this.addWimndowListneter(this);

..

 public void windowClosing(WindowEvent evt) {
    // do here all your stuff hwen you close the dialog yourself
  }
//  and you need to many other methods of WindowListener with empty body


}

Open in new window

0
for_yanCommented:
No, you need all those cleaning stuff you are doing whine you closing yourself in WindowClosing
if there is a possibility they will close through the X
0
jkteaterAuthor Commented:
You are right - I really don't care if they click the top button.  It is ok to have the same data when the dialog opens back up.  but the textarea is appending the text every time it is opened.  I would have thought it would have stayed the same
0
jkteaterAuthor Commented:
I think there is a problem with the method getting the string
0
for_yanCommented:
No, you need to care - you need to assign null to your dialog, so that  and also take care of this string in the model
What ikind of problem ther may be - the model needs to be cleaned - next time you open dialog it again gets the same string form the same method
0
jkteaterAuthor Commented:
The problem was in the method - I was not resetting the string buffer so it was appending
0
CEHJCommented:
>> but the textarea is appending the text every time it is opened.

Did you post the right code? The code you posted shows a new JTextArea being created
0
jkteaterAuthor Commented:
for_yan

I do understand about the window close.  That will be a question in the future.
0
for_yanCommented:
Sure, taht's what it was, but in any case you should ensure that no matter how they close the dialog
the satus of your program should correspond to the closed dalog.
WindowListener is exactly devised for that purpose
0
CEHJCommented:
:)

Still not sure if you posted the right code though. If you're actually USING those components you create, it shouldn't re-append
0
jkteaterAuthor Commented:
What was happening in my method myModel.commentToString()

each time the method was called it just appended the same results to back to the string buffer.  That is the reason it was growing with the same values.  So I had to reset String Buffer, so when the method was called it was appending to empty one.
0
for_yanCommented:
So that's what I was writing you from the very begining - that you need to reset in your model
0
for_yanCommented:
Why did you accept in such a strange way?
0
CEHJCommented:
Yes, it looks like you accepted the wrong answer there. Should have been http:#37058582

I'll get that seen to
0
jkteaterAuthor Commented:
I did not understand what you meant until just now.
0
for_yanCommented:
Thanks a lot, CEHJ
That is really a new development;)
I very much appreciate it.
0
jkteaterAuthor Commented:
Yes - I did accept the wrong answer - very sorry about that
0
for_yanCommented:
we can still leave it as it is, not to bother moderators
200 points will not make a diifference and this question would hardly be interesting to some third party, so
in this sense it does not matter which will be the solution
0
for_yanCommented:
Thanks, CEHJ and Modalot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.