首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DefaultStyledDocument.styleChanged(Style style)可能无法及时运行?

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

Stack Overflow用户
提问于 2010-05-07 04:52:51
回答 2查看 269关注 0票数 0

我遇到了一个扩展javax.swing.text.DefaultStyledDocument的类的间歇性问题。正在将此文档发送到打印机。大多数情况下,文档的格式看起来是正确的,但有时不正确。看起来格式中的某些更改尚未应用。

我看了一下DefaultStyledDocument.styleChanged(Style style)代码:

代码语言:javascript
复制
/**
 * 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();
    }
}
}

调用的是SwingUtilities.invokeLater(updateRunnable)而不是invokeAndWait(updateRunnable),这是否意味着我不能指望在呈现文档之前我的格式更改就会出现在文档中?

如果是这样的话,有没有办法确保我在更新之前不会继续渲染?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-10 23:51:47

您将在代码的末尾看到一个fireChangedUpdate(dde);。尝试将自己附加为DocumentListener。在DocumentListener.changedUpdate方法中,您应该保存以打印包含所有更改的文档。

票数 2
EN

Stack Overflow用户

发布于 2010-05-07 05:14:00

我也遇到过类似的问题。

为了解析,我在swing文本中设置了一些内容后启动,一个空的invokeLater,当这个invokeLater完成时,我希望稍后的swing文本调用也完成。

我的代码可能比我的英语更好:

代码语言:javascript
复制
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.
  }
});

很可怕,但这是工作,抱歉。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2784420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档