0

我遇到了一个扩展类的间歇性问题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)意味着我不能指望在呈现文档之前出现在文档中的格式更改?

如果是这种情况,有没有办法确保在更新发生之前我不会继续渲染?

4

2 回答 2

2

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

于 2010-05-10T15:51:46.627 回答
1

我有类似的问题。

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

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

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

这是可怕的,但它的工作,对不起。

于 2010-05-06T21:13:59.657 回答