Igr
Igr

Reputation: 1045

Clear text from a JTextArea

I'm writing text on a jPanel: when I press a button it shows text about that button, when i press another one shows text about that one and so on...

The text area is created like this:

    JTextArea log = new JTextArea(1,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);

    add(logScrollPane, BorderLayout.CENTER);

When I display some text with:

log.append("No file path specified");

I'm not able to delete the previous text. For esample if I press twice the same button i get the string

"No file path specifiedNo file path specified"

I'm not able to clear the text area to display only the new string. I've tryed with:

log.removeAll();

before the log.append() But didn't work.

Upvotes: 2

Views: 68511

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

Use either log.setText(null) or log.setText(""), same thing

Rather the appending text, you should try log.setText("No file path specified");, which will replace the current contents with the new String (Thanks Dave)

You might like to take some time to read through Using text components for more details

Upvotes: 9

omartin
omartin

Reputation: 135

try setText from JTextComponent super class

setText("")

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

Upvotes: 2

Related Questions