title: JAVA Appending stringbuilder to textarea, not replacing textarea just adding to it (need to replace instead of adding)
body: <p>I have a stringbuilder to load up some lists of strings, my problem is when I add to the textarea I want to replace what was there and not just add to it.. I'm posting all of my code which is a lot so I'll further explain if anyone needs additional information. I THINK the problem is when I convert the stringbuilder with the .toString() method when appending, that may be adding to the list each time and if that is the case I have no clue how to solve this...</p>
  
  <pre><code>import java.awt.GridLayout;
  import javax.swing.JFrame;
  import java.awt.event.*;
  import javax.swing.*;
  import java.awt.*;
  import java.io.*;
  import java.util.Scanner;
  import java.util.Arrays;
  import javax.swing.JComboBox;
  
  
  public class Lab10 extends JPanel
  {
    private StringBuilder string, string2, string3, string4; //loads SuperStrings faster by appending all at once
    private JRadioButton occurrence, alphabetical;
    private JPanel text;
    private JComboBox&lt;Integer&gt; input;
    private JLabel label, file1,file2, unique, unique2;
    private JButton load, go,go2;
    private CountLinkedList&lt;SuperString&gt; words, words3; //Change impliments CountList to extends BinaryCountTree
    private OrderedLinkedList&lt;SuperString&gt; words2, words4;//Change impliments CountList to extends BinaryCountTree
    private String filename,filename2;
    private int width = 450;
    private int height = 550;
    private TextArea textarea,textarea2;
    Scanner scan;
  
    public Lab10()
    {
      string = new StringBuilder();
      string2 = new StringBuilder();
      string3 = new StringBuilder();
      string4 = new StringBuilder();
      ButtonListener listener = new ButtonListener();
      Button2Listener listener2 = new Button2Listener();
      Integer [] select = {1,2,3,4};
      input = new JComboBox&lt;Integer&gt;(select);
      text = new JPanel(new GridLayout(1,2));
      go = new JButton("Select Text File 1: ");
      go2 = new JButton("Select Text File 2: ");
      label = new JLabel("N: " );
      unique = new JLabel("");
      unique2 = new JLabel("");
      file1 = new JLabel("");
      file1.setFont(new Font("Helvetica",Font.PLAIN,24));
      unique.setFont(new Font("Helvetica",Font.PLAIN,24));
      file2 = new JLabel("");
      file2.setFont(new Font("Helvetica",Font.PLAIN,24));
      unique2.setFont(new Font("Helvetica",Font.PLAIN,24));
  
      occurrence= new JRadioButton("Occurrence");
      occurrence.setMnemonic(KeyEvent.VK_O);
      occurrence.addActionListener(listener);
      occurrence.addActionListener(listener2);
      occurrence.setSelected(true);
  
  
      alphabetical = new JRadioButton("Alphabetical");
      alphabetical.setMnemonic(KeyEvent.VK_A);
      alphabetical.addActionListener(listener);
      alphabetical.addActionListener(listener2);
  
      ButtonGroup group = new ButtonGroup();
      group.add(occurrence);
      group.add(alphabetical);
  
      go.addActionListener(listener);
      go2.addActionListener(listener2);
      input.addActionListener(listener);
      input.addActionListener(listener2);
  
      textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
      textarea2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
      textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
      textarea2.setFont(new Font("Helvetica",Font.PLAIN,24));
      textarea.setPreferredSize(new Dimension(width,height));
      textarea2.setPreferredSize(new Dimension(width,height));
      setPreferredSize(new Dimension(1000,700));
      text.add(textarea);
      text.add(textarea2);
      add(occurrence);
      add(alphabetical);
      add(label);
      add(input);
      add(go);
      add(file1);
      add(unique);
      add(go2);
      add(file2);
      add(unique2);
  
      add(text);
  
      textarea.setText("No File Selected");
      textarea2.setText("No File Selected");
  
  
    }
      public class ButtonListener implements ActionListener //makes buttons do things
      {
        JFileChooser chooser = new JFileChooser("../Text");
  
  
        public void actionPerformed(ActionEvent event)
        {
         Integer N = input.getSelectedIndex()+1;
  
          if(event.getSource() == go)
        {
  
          int returnvalue = chooser.showOpenDialog(null);
  
      if(returnvalue == JFileChooser.APPROVE_OPTION)
      {
        try
        {
          File file = chooser.getSelectedFile();
          String text1= file.getName();
          file1.setText(text1);
          filename = file.getName();
          System.err.println(filename);
          scan = new Scanner(file);
  
        }
        catch (IOException e)
        {
          System.err.println("IO EXCEPTION");
          return;
        }       
      }
      else
      {
        return;
      }
  
  
       String[] storage = new String[N];
      words = new CountLinkedList&lt;SuperString&gt;();
      words2 = new OrderedLinkedList&lt;SuperString&gt;();
     for(int i=1;i&lt;N;i++)
       storage[i] = scan.next().toLowerCase().replaceAll("[^A-Za-z0-9]", "")
                    .replaceAll("[.,':]","");
  
      while(scan.hasNext())
      {
        for(int i=0;i&lt;=N-2;i++)
          storage[i] = storage[i+1];
        storage[N-1] = scan.next().toLowerCase();
        storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
                    .replaceAll("[.,':]","");
        SuperString ss = new SuperString(storage);
        SuperString ss2= new SuperString(storage);
        words.add(ss );
        words2.add(ss2 );
      }
  
      textarea.setText("");
          }
      SuperString[] ss = new SuperString[words.size()];
      SuperString[] ss2 = new SuperString[words2.size()];
      int i=0;
      int count =0, count2= 0;
  
  
      for(SuperString word: words)
      {
        ss[i] = word;
        i++;
      }
      int j=0;
      for(SuperString word: words2)
      {
        ss2[j] = word;
        j++;
      }
  
  
      Arrays.sort(ss, new SuperStringCountOrder());
  
      for(SuperString word : ss)
      {
        count++;
        string.append(Integer.toString(count)+ "       "+ word+ "\n");
      }
      if(occurrence.isSelected())
      {
        string.replace(0,0,"");
       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())
      {
                   string.replace(0,0,"");
        textarea.setText("");
        textarea.append("    "+filename+" has wordcount: "+words.size()+
        "\n-------------------------\n\n");
        textarea.append(string2.toString());
  
      }
       unique.setText("Unique Count: "+ Integer.toString(words.size()));
  
        }
      }
  
  
      public class Button2Listener implements ActionListener
      {
         JFileChooser chooser = new JFileChooser("../Text");
  
  
        public void actionPerformed(ActionEvent event)
        {
         Integer N = input.getSelectedIndex()+1;
  
           if(event.getSource() == go2)
        {
  
          int returnvalue = chooser.showOpenDialog(null);
  
      if(returnvalue == JFileChooser.APPROVE_OPTION)
      {
        try
        {
          File file = chooser.getSelectedFile();
          String text2= file.getName();
          file2.setText(text2);
          filename2 = file.getName();
          System.err.println(filename);
          scan = new Scanner(file);
  
        }
        catch (IOException e)
        {
          System.err.println("IO EXCEPTION");
          return;
        }       
      }
      else
      {
        return;
      }
  
       String[] storage = new String[N];
      words3 = new CountLinkedList&lt;SuperString&gt;();
      words4 = new OrderedLinkedList&lt;SuperString&gt;();
     for(int i=1;i&lt;N;i++)
       storage[i] = scan.next().toLowerCase().replace(",","").replace(".","");
  
      while(scan.hasNext())
      {
        for(int i=0;i&lt;=N-2;i++)
          storage[i] = storage[i+1];
        storage[N-1] = scan.next().toLowerCase();
        storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
                    .replaceAll("[.,':]","");
        SuperString ss = new SuperString(storage);
        SuperString ss2= new SuperString(storage);
        words3.add(ss );
        words4.add(ss2 );
      }
      textarea2.setText("");
           }
      SuperString[] sstwo = new SuperString[words3.size()];
      SuperString[] ss2two = new SuperString[words4.size()];
      int i=0;
      int count =0, count2= 0;
  
      for(SuperString word2: words3)
      {
        sstwo[i] = word2;
        i++;
      }
  
      int j=0;
      for(SuperString word2: words4)
      {
        ss2two[j] = word2;
        j++;
      }  
  
      Arrays.sort(sstwo, new SuperStringCountOrder());
  
          for(SuperString word2 : sstwo)
      {
        count++;
        string3.append(Integer.toString(count)+ "       "+ word2+ "\n");
      }
      if(occurrence.isSelected())
      {
       textarea2.setText("");
       textarea2.append("    "+filename2+" has wordcount: "+words3.size()+
        "\n-------------------------\n\n");
       textarea2.append(string3.toString());
      }
  
       for(SuperString word2 : ss2two)
      {
         count2++;
          string4.append(count2+"   "+ "  "+word2+"\n");     
      }
       if(alphabetical.isSelected())
            {
         textarea2.setText("");
         textarea2.append("    "+filename2+" has wordcount: "+words3.size()+
        "\n-------------------------\n\n");
         textarea2.append(string4.toString());
         }
  
       unique2.setText("Unique Count: "+ Integer.toString(words3.size()));
  
        }
      }
  
  
  
     public static void main(String[] arg)
    {
      JFrame frame = new JFrame("Lab 10");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      frame.getContentPane().add(new Lab10());
      frame.pack();
      frame.setVisible(true);
    }
  }
  </code></pre>