Solved

TextArea replace text

Posted on 2011-10-31
28
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 8
  • 5
28 Comments
 
LVL 47

Expert Comment

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

Expert Comment

by:CEHJ
ID: 37058576
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
ID: 37058582

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 47

Expert Comment

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

Author Comment

by:jkteater
ID: 37058616
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
ID: 37058630
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
ID: 37058638
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
ID: 37058659
>>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
ID: 37058666
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
ID: 37058674
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
ID: 37058680
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
ID: 37058697
I think there is a problem with the method getting the string
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37058725
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
 

Author Comment

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

Expert Comment

by:CEHJ
ID: 37058749
>> 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
ID: 37058751
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
ID: 37058756
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
ID: 37058764
:)

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
ID: 37058806
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
ID: 37058819
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
ID: 37058828
Why did you accept in such a strange way?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37058882
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
ID: 37058888
I did not understand what you meant until just now.
0
 
LVL 47

Expert Comment

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

Author Comment

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

Expert Comment

by:for_yan
ID: 37058924
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
ID: 37067087
Thanks, CEHJ and Modalot.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 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