How to clear JTextArea?

Lord Rixuel picture Lord Rixuel · Apr 3, 2013 · Viewed 72k times · Source

I'm trying to clear the JTextArea.

Currently, I'm using

jtextarea.setText(null);

What is the difference if I use

jtextarea.setText("");

Answer

Kevin S picture Kevin S · Apr 3, 2013

There is no difference. They both have the effect of deleting the old text. From the java TextComponent page:

setText

  public void setText(String t)

  Sets the text of this TextComponent to the specified text. If the text is null
  or empty, has the effect of simply deleting the old text. When text has been
  inserted, the resulting caret location is determined by the implementation of
  the caret class.

  Note that text is not a bound property, so no PropertyChangeEvent is fired when
  it changes. To listen for changes to the text, use DocumentListener.

  Parameters:
      t - the new text to be set
  See Also:
      getText(int, int), DefaultCaret