-2

So string.append and string2.append are StringBuilder objects I made to load words from a text file faster, my problem is that when I switch from radio button occurrence and alphabetical it just adds to the text area instead of replacing the previous list. Any ideas on how to fix this?

for(SuperString word : ss)
{
  count++;
  string.append(Integer.toString(count)+ "       "+ word+ "\n");
  string.toString();
}
if(occurrence.isSelected())
{
 textarea.setText("");
 textarea.append("    "+filename+" has wordcount: "+words.size()+
  "\n-------------------------\n\n");
 textarea.append(string.toString());
}


 for(SuperString word : ss2)
{
   count2++;
   string2.append(Integer.toString(count2)+ "       "+ word.toString()+ "\n");  
}
     if(alphabetical.isSelected())
{
  textarea.setText("");
  textarea.append(string2.toString());

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `"when I switch from radiobutton occurrence and alphabetical"` -- consider explaining this as if we don't already know what your program is doing or is supposed to be doing. e.g., what in blazes is "radiobutton occurence"? – Hovercraft Full Of Eels Dec 02 '14 at 03:35
  • When I click on occurence my StringBuilder (string) appends to textarea that has presorted the txt file by occurence of words, when I click alphabetical my StringBuilder (string2) appends to textarea that has been presorted by alphebtical strings of the txt file.. when I do this as is it just adds to the text area instead of replacing what's there, i.e. making the list longer and longer – Cpt.Awesome Dec 02 '14 at 03:37
  • Oh sorry, JRadioButton is like a clickable 'type' I guess. so based on which is selected a certain list will be displayed – Cpt.Awesome Dec 02 '14 at 03:40
  • 1
    Maybe it's me, but I'm totally lost. Please consider creating and posting a [minimal example program](http://stackoverflow.com/help/mcve), a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification. – Hovercraft Full Of Eels Dec 02 '14 at 03:42
  • 1
    Maybe `textarea.replaceRange()` – trashgod Dec 02 '14 at 03:43
  • There are several classes and to implement my driver class... Basically I need my textarea to reset each time I select occurrence or alphabetical BUT it is not doing so even though I have a textarea.setText("") command in each instance of isSelected(); ......Does that make more sense? – Cpt.Awesome Dec 02 '14 at 03:44
  • Nahh, that's not working.. like replaceRange("",0,0) ..it requires a string, int , int – Cpt.Awesome Dec 02 '14 at 03:48

1 Answers1

2

You may be looking for replaceRange(), which "Simply does a delete if the new string is null or empty." Then you can insert() the new text at position 0.

textarea.replaceRange(null, 0, 0);
textarea.insert(string2.toString(), 0);

Addendum: This above does not work.

Both replaceRange() and setText() correctly clear the text area when supplied with a null or empty String. The latter is illustrated in this complete example. As you may have a problem elsewhere in your code, verify that all Swing GUI objects are constructed and manipulated only on the event dispatch thread. Note in particular that append() is no longer thread safe.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I just want to clear and replace the textarea, appreantly this is hard to to using stringbuilder. this above does not work – Cpt.Awesome Dec 07 '14 at 01:51
  • I've elaborated above. If you still have a problem, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that reproduces the problem. – trashgod Dec 07 '14 at 05:31