8

In a program I'm working on, a textfield has to display some text at some point.

output.setText( outputString );
outputString = "";

output is a JTextField. These lines of code are in a method, and when it is called the first time, it works perfectly fine. However, when it is called another time, the original outputString text still remains. Why does this happen, and how can I fix it?

Okay, I think it happens because strings are immutable. The thing is, outputString never changes, so it still has the text from the initial method call.

How do I, somehow, change the text in the string?

kullalok
  • 813
  • 2
  • 13
  • 19
  • Okay guys, I just tried that same answer that was given to me 5 times. It doesn't work. I put output.setText( "" ); immediately before output.setText( outputString );, but outputString doesn't change, so it still contains the earlier chunk of text. – kullalok Jun 19 '12 at 13:43
  • "immediately before output.setText( outputString )". Of course that would not clear the text, you are just overwriting it. If you want the text cleared, you must call setText("") AFTER. – Chris Dargis Jun 19 '12 at 13:48
  • 2
    Well... If you do output.setText("") and then immediatly after that do output.setText(outputString), then your textfield will contain whatever outputString contains. Your TextField will be empty, but will be overridden instantly by your second call. If you want it the TextField to be empty, only call output.setText("") – John Snow Jun 19 '12 at 13:48

10 Answers10

5

Setting the text to the contents of your variable does not set up a permanent relationship between that variable and that text field, if you want to clear the text, you could use

output.setText("");
Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
4

You have to clear the text from the JTextField object. Sending a variable as an argument with setText() does not tie that variable to the object. It simply copies the string.

output.setText("");

If the text is null or empty, has the effect of simply deleting the old text.

http://docs.oracle.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText%28java.lang.String%29

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
2

I think you are misunderstanding something very important. Java Strings are immutable that means that you can't change them.

"How do I, somehow, change the text in the string?"

You can't. The text in the string cannot be changed. It is immutable. Any "solution" that involves changing the text in a String WON'T WORK in Java. (Got that?)

When you do this:

output.setText(outputString);
outputString = "";

the assignment does not change the value that is displayed in the text field. It just changes the String that the local variable outputString refers to.

And when you do this:

output.setText(""); 
output.setText(outputString);

it does not cause outputString to change. It just changes the displayed text to nothing and then immediately changes it to whatever outputString currently refers to.

If you want to change the value displayed in the text field to nothing, you JUST do this:

output.setText("");

Perhaps the other thing that you've got wrong in your thinking is that you think that this:

output.setText(outputString);

sets up a relationship between the text field output and the variable outputString ... such that when the user types into the field, the outputString variable is magically updated. That is NOT so. In fact, it CANNOT be so, because you cannot pass the address of variable.

In fact, output.setText(outputString); just passes the value of outputString to the text box object. If and when the user types something into the box, the characters are stored somewhere else, and only returned to your code ... as a new String ... when your code calls output.getText().

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1
output.setText("");

Will solve this problem

Alpesh Prajapati
  • 1,593
  • 2
  • 18
  • 38
1

Why does this happen?

String are immutable.

There is always a new reference to the String. Thus, when you call outputString = "";, the new String is created.

How can I fix it?

Call output.setText("");.

d1e
  • 6,372
  • 2
  • 28
  • 41
1

What your code is doing, is first setting the contents of the text field to the contents of your outputString variable in: output.setText( outputString );.

Your subsequent code line does not change the String object, which is the content of your text field, but rather set your outputString variable to refer to the empty string variable. Strings being immutable in java, it is impossible to have changes to one string variable reflect to an other.

As a number of people have stated, you need to set your text fields contents by calling: jTextFieldVar.setText(""); or jTextFieldVar.setText(null);.

Kallja
  • 5,362
  • 3
  • 23
  • 33
0

Are you setting the outputstring to "" after you try to set the JTextField's text? If so you should try to do something like this:

  output.setText("");
Logard
  • 1,494
  • 1
  • 13
  • 27
0

I think what you are looking for is:

 output.setText("");
DP Greveling
  • 409
  • 5
  • 12
0

this will work out just fine.

output.setString("");

Djkgotso
  • 130
  • 2
  • 4
  • 15
0

Other answers pointing to the fact that Strings are immutable are accurate.

But if you want to have the functionality of "clearing a String", you can use a StringBuffer instead and call this on it:

stringBuffer.delete(0, stringBuffer.length());
cellepo
  • 4,001
  • 2
  • 38
  • 57