elite_perry
elite_perry

Reputation: 45

Cannot setText of JTextArea before appending

I'm having some trouble setting text of a JTextArea and then appending it. I basically just want to clear the text and then append some other text after that. The result is that the text is not clearing and the text is being appended. I've provided some mock up code to show what I basically have.

public Constructor(){
textArea = new JTextArea();
textArea.setText("Wow");

someBoolean = false;

someString = "Oh";

}

public someOtherMethod(){
   if(!someBoolean){

     if(textArea.equals("Wow"){
       textArea.setText("");
      } else {

   textArea.append(someString+"\n");
   }

 }
}

Upvotes: 0

Views: 275

Answers (1)

stuhlo
stuhlo

Reputation: 1507

textArea is an object of the class JTextArea. Therefore, your condition textArea.equals("Wow") is inappropriate. You are comparing JTextArea object to String object what always returns false. Proper way how to compare text inside JTextArea is following:

if(textArea.getText().equals("Wow"))

By the way, do not forget to call setText(...) on event dispatch thread:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        textArea.setText(...);
    }
});

Upvotes: 1

Related Questions