title: Java GUI freezing because of insertString method?
body: <p>Have you ever heard about a GUI freezing because of repeated calls to the method javax.swing.Document.insertString?</p>
  
  <p>There is my code:</p>
  
  <pre><code>private int insertNotation(GameNode startNode, StyledDocument doc, int pos) {
  
      String s = "";
      int startPos = pos;
      boolean isContinuous = false;
  
      Style boldStyle, regularStyle, commentStyle, currentNodeStyle, nagStyle, grayStyle;
      grayStyle = notationTextPane.getStyle("gray");
      GameNode currentNode = history.getCurrentGameNode();
      if ((currentNode.isLeaf() &amp;&amp; startNode == currentNode.getParent()) || startNode == currentNode) {
  
      try {
          if (startNode.getComment().length() &gt; 0) {
              s = startNode.getComment() + " ";
              commentStyle.addAttribute("gameNode", startNode);
              doc.insertString(pos, s, commentStyle);
              pos += s.length();
          }
          for (int n = 0; n &lt; startNode.getChildCount(); n++) {
              GameNode node = (GameNode) startNode.getChildAt(n);
              boolean isCurrentNode = (node == currentNode);
              if (node.isLeaf()) {
                  if (node.isWhiteMove()) {
                      s = node.getFullMoveNumber() + ". ";
                      boldStyle.addAttribute("gameNode", node);
                      doc.insertString(pos, s, boldStyle);
                      pos += s.length();
                      s = node.getMove();
                      Style style = isCurrentNode ? currentNodeStyle : regularStyle;
                      style.addAttribute("gameNode", node);
                      doc.insertString(pos, s, style);
                      pos += s.length();
                      isContinuous = true;
                  } else {
                      if (isContinuous) {
                          s = node.getMove();
                          Style style = isCurrentNode ? currentNodeStyle : regularStyle;
                          style.addAttribute("gameNode", node);
                          doc.insertString(pos, s, style);
                          pos += s.length();
                      } else {
                          isContinuous = true;
                          s = node.getFullMoveNumber() + "... ";
                          boldStyle.addAttribute("gameNode", node);
                          doc.insertString(pos, s, boldStyle);
                          pos += s.length();
                          s = node.getMove();
                          Style style = isCurrentNode ? currentNodeStyle : regularStyle;
                          style.addAttribute("gameNode", node);
                          doc.insertString(pos, s, style);
                          pos += s.length();
                      }
                  }
                 doc.insertString(pos++, " ", regularStyle);
          }
      } catch (BadLocationException e) {
       e.printStackTrace();
      }
      return pos - startPos;
  }
  </code></pre>
  
  <p>I simplified it a lot but as you can see, there are many calls to the insertString() method in my 'doc' StyledDocument variable. 
  This StyledDocument object is added in a JTabbedPane.</p>
  
  <p>I have read <a href="http://www.javaperformancetuning.com/tools/jprofiler/index.shtml" rel="nofollow noreferrer">here</a> (in Performance Analysis section) that <code>javax.swing.Document.insertString</code> method is extremely slow (here over 1 ms per call). </p>
  
  <p>Can repeated calls to it freeze the GUI?</p>