Java GUI冻结是因为insertString方法? [英] Java GUI freezing because of insertString method?

查看:149
本文介绍了Java GUI冻结是因为insertString方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有没有听说过因为重复调用方法javax.swing.Document.insertString而导致GUI冻结?

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

我的代码是:

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;
}

我简化了很多,但正如你所看到的,有很多电话要求我的'doc'StyledDocument变量中的insertString()方法。
这个StyledDocument对象添加在JTabbedPane中。

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.

我读过这里(在性能分析部分中) javax.swing.Document.insertString 方法非常慢(这里结束)每次通话1 ms)。

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

可以重复调用它来冻结GUI吗?

Can repeated calls to it freeze the GUI?

推荐答案

每当你在主GUI线程中执行的操作很慢,你将冻结GUI。它根据处理事件重绘。想象一下,您的事件处理代码是在一个while循环中从队列中拉出事件 - 如果您没有从函数返回,则无法处理下一个事件。

Whenever you do something slow in the main GUI thread, you will freeze the GUI. It redraws based on processing events. Imagine that your event handling code is in a while loop pulling events off a queue -- if, you don't return from your function, the next event can't be processed.

考虑在后台线程中进行长时间运行或慢速处理。

Consider doing long-running or slow processing in a background thread.

参见此文章: http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

这篇关于Java GUI冻结是因为insertString方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆