Solved

TextArea replace text

Posted on 2011-10-31
28
298 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 80
micro services spring boot application error 3 149
github account with ecipse 1 42
Java Options in Zimbra Server 2 47
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 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

20 Experts available now in Live!

Get 1:1 Help Now