Java GUI intent due to insertString method?

Have you ever heard of the GUI freezing due to repeated calls to the javax.swing.Document.insertString method?

There is my 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() && startNode == currentNode.getParent()) || startNode == currentNode) {

    try {
        if (startNode.getComment().length() > 0) {
            s = startNode.getComment() + " ";
            commentStyle.addAttribute("gameNode", startNode);
            doc.insertString(pos, s, commentStyle);
            pos += s.length();
        }
        for (int n = 0; n < 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;
}

I simplified it, but as you can see, there are many calls to the insertString () method in my "doc" StyledDocument variable. This StyledDocument is added to the JTabbedPane.

I read here (in the Performance Analysis section) that the method is javax.swing.Document.insertStringvery slow (over 1 ms per call here).

Can repeated calls to freeze its GUI?

+1
source share
6 answers

, . SwingWorker.

. . .

BlockingQueue<String> toAdd = new LinkedBlockingQueue<String>();
toAdd.add("Some text");
toAdd.add("Some more text");    

SwingWorker, .

new SwingWorker<Void, String>() {
   // Implementation of 'process' and 'doInBackground' methods to go here.
}.execute();

doInBackground , , Thread Dispatch .

  public String doInBackground() {
    while (!Thread.interrupted()) {
      List<String> l = new LinkedList<String>();
      String s = toAdd.poll();

      if (s == null) {
        publish(l.toArray(new String[l.size()]));
        l.clear();
      } else {
        l.add(s);
      }
    }

    // Thread interrupted but publish anything pending before returning.
    if (!l.isEmpty()) {
      publish(l.toArray(new String[l.size()]));
    }

    return null;
  }

, process. Swing . chunks , StringBuilder, ( ).

  public void process(String... chunks) {
    StringBuilder sb = new StringBuilder();
    for (String chunk : chunks) {
      sb.append(chunk);
    }

    // Insert sb.toString() into buffer HERE
  }
+2

( ) ?

, , , ? insertString , , GUI.

, , . , , setDocument, .

.

0

, SwingWorker GUI: javax.swing.Document.insertString SwingWorker.

: . SwingWorker.

:

 new SwingWorker<Void, Void>()
 {
  @Override
  protected Void doInBackground() throws Exception
   {
    visualBoard.update(); // here your method call, deported from the GUI thread. 
    return null;
   }

 }.execute();

!

0

. ; (CPU = 0%).

: http://tinybrain.de/1000816

, , - "".

, JTextPane ...

: JTextArea.append! http://tinybrain.de/1000822

, , AWT-. main() SwingUtilities.invokeLater . .

GUI "awt {}" ( JavaX SwingUtilities.invokeLater), , : http://tinybrain.de/1000823.

0

. .

-2

Source: https://habr.com/ru/post/1693554/


All Articles