Clear text from a JTextArea

61,303

Solution 1

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

Solution 2

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

Share:
61,303
Igr
Author by

Igr

Updated on June 11, 2020

Comments

  • Igr
    Igr about 4 years

    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.