Solved

TextArea replace text

Posted on 2011-10-31
28
297 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:jkteater
  • 14
  • 8
  • 5
28 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You need to clear what was in your model when you close the dilaog
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If the component has not been changed between closing and reopening, it won't change its text either
0
 
LVL 47

Accepted Solution

by:
for_yan earned 50 total points
Comment Utility

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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Is your textArae in your dialog which was cloased and then again opened - correct ?
0
 

Author Comment

by:jkteater
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
with the X in the top - you need to catch that even and
do the same you are douingwhen cloasing
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 

Author Comment

by:jkteater
Comment Utility
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
 

Author Comment

by:jkteater
Comment Utility
I think there is a problem with the method getting the string
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jkteater
Comment Utility
The problem was in the method - I was not resetting the string buffer so it was appending
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>> 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
 

Author Comment

by:jkteater
Comment Utility
for_yan

I do understand about the window close.  That will be a question in the future.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)

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
 

Author Comment

by:jkteater
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
So that's what I was writing you from the very begining - that you need to reset in your model
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Why did you accept in such a strange way?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes, it looks like you accepted the wrong answer there. Should have been http:#37058582

I'll get that seen to
0
 

Author Comment

by:jkteater
Comment Utility
I did not understand what you meant until just now.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Thanks a lot, CEHJ
That is really a new development;)
I very much appreciate it.
0
 

Author Comment

by:jkteater
Comment Utility
Yes - I did accept the wrong answer - very sorry about that
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Thanks, CEHJ and Modalot.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now