How to clear a line?

In the program I'm working on, the text box should display some text at some point.

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

is a JTextField. These lines of code are in the method, and when it is called for the first time, it works fine. However, when it is called a different time, the source text of outputString remains. Why is this happening, and how can I fix it?

Well, I think this is because the lines are immutable. The fact is that outputString never changes, so it still has the text from the initial method call.

How can I somehow change the text in a line?

+5
source share
10 answers

, ,

output.setText("");
+5

, - . Java , , .

" - ?"

. . . "", , Java. (?)

:

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

, . , outputString.

:

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

outputString. , , outputString.

, , , :

output.setText("");

, , , , , :

output.setText(outputString);

output outputString... , , outputString. . , , .

, output.setText(outputString); outputString . - , ... ... output.getText().

+2

?

.

String. , outputString = ""; .

?

output.setText("");.

+1

, outputString : output.setText( outputString );.

String, , outputString . , java, , .

, , : jTextFieldVar.setText(""); jTextFieldVar.setText(null);.

+1

outputstring "" , JTextField? , - :

  output.setText("");
0
output.setText("");

0

, :

 output.setText("");
0

.

output.setString( "");

0

Other answers indicating that the Lines are immutable are accurate.

But if you want to have the "clear string" functionality, you can use a StringBuffer and call it on it :

stringBuffer.delete(0, stringBuffer.length());
0
source

All Articles