简体   繁体   English

DefaultStyledDocument.styleChanged(Style style)可能无法及时运行?

[英]DefaultStyledDocument.styleChanged(Style style) may not run in a timely manner?

I'm experiencing an intermittent problem with a class that extends javax.swing.text.DefaultStyledDocument . 我遇到了一个扩展javax.swing.text.DefaultStyledDocument的类间歇性问题。 This document is being sent to a printer. 该文档正在发送到打印机。 Most of the time the formatting of the document looks correct, but once in a while it doesn't. 大多数情况下,文档的格式看起来正确,但有时却不正确。 It looks like some of the changes in the formatting have not been applied. 看起来格式中的某些更改尚未应用。

I took a look at the DefaultStyledDocument.styleChanged(Style style) code: 我看了看DefaultStyledDocument.styleChanged(Style style)代码:

/**
 * Called when any of this document's styles have changed.
 * Subclasses may wish to be intelligent about what gets damaged.
 *
 * @param style The Style that has changed.
 */
protected void styleChanged(Style style) {
    // Only propagate change updated if have content
    if (getLength() != 0) {
        // lazily create a ChangeUpdateRunnable
        if (updateRunnable == null) {
            updateRunnable = new ChangeUpdateRunnable();
        }

        // We may get a whole batch of these at once, so only
        // queue the runnable if it is not already pending
        synchronized(updateRunnable) {
            if (!updateRunnable.isPending) {
                SwingUtilities.invokeLater(updateRunnable);
                updateRunnable.isPending = true;
            }
        }
    }
}

/**
 * When run this creates a change event for the complete document
 * and fires it.
 */
class ChangeUpdateRunnable implements Runnable {
    boolean isPending = false;

public void run() {
        synchronized(this) {
            isPending = false;
        }

    try {
    writeLock();
    DefaultDocumentEvent dde = new DefaultDocumentEvent(0,
                      getLength(),
                      DocumentEvent.EventType.CHANGE);
    dde.end();
    fireChangedUpdate(dde);
    } finally {
    writeUnlock();
    }
}
}

Does the fact that SwingUtilities.invokeLater(updateRunnable) is called, rather than invokeAndWait(updateRunnable) , mean that I can't count on my formatting changes appearing in the document before it is rendered? 调用SwingUtilities.invokeLater(updateRunnable)而不是invokeAndWait(updateRunnable)的事实是否意味着我无法依靠呈现在文档上的格式更改进行渲染?

If that is the case, is there a way to ensure that I don't proceed with rendering until the updates have occurred? 如果是这种情况,是否有办法确保在更新发生之前不进行渲染?

You see a fireChangedUpdate(dde); 您会看到fireChangedUpdate(dde); at the end of the code. 在代码末尾。 Try to append yourself as a DocumentListener . 尝试将自己追加为DocumentListener Inside the DocumentListener.changedUpdate method you should be save to print your document with all changes included. DocumentListener.changedUpdate方法内部,应保存您的内容以打印包含所有更改的文档。

I have had similar problem. 我有类似的问题。

To resolv, I launch after set something in a swing text, an empty invokeLater, and when this invokeLater is done, I hope the swing text invoke later is done. 为了解决这个问题,我在设置了swing文本中的内容之后,启动了一个空的invokeLater,并在完成该invokeLater之后,希望稍后完成swing文本调用。

My code is perhaps better than my english : 我的代码也许比我的英语更好:

doc.formatSomethingWhichPerhapsLaunchInvokeLater();
EventQueue.invokeLater(new java.lang.Runnable()
{
  public void run()
  {
    // at this point, I hope all swing text stuff is finish. 
    // Until now, it's the case.
  }
});

It's horribel, but it's work, sorry. 很恐怖,但是很有效,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM