繁体   English   中英

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

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

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

我看了看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();
    }
}
}

调用SwingUtilities.invokeLater(updateRunnable)而不是invokeAndWait(updateRunnable)的事实是否意味着我无法依靠呈现在文档上的格式更改进行渲染?

如果是这种情况,是否有办法确保在更新发生之前不进行渲染?

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

我有类似的问题。

为了解决这个问题,我在设置了swing文本中的内容之后,启动了一个空的invokeLater,并在完成该invokeLater之后,希望稍后完成swing文本调用。

我的代码也许比我的英语更好:

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

很恐怖,但是很有效,抱歉。

暂无
暂无

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

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